Python project and Tkinter menu

Asked

Viewed 419 times

0

Well my little project with python and the Tkinter library , I’m trying to make a registration system but I’m a little new in this part of object orientation in python, and I think this is the part that’s giving me the error! Could give me a hand on what to do:

from tkinter import *
from functools import partial
import tkinter.messagebox        

#-*- coding:UTF-8 -*-
from tkinter import *
import sys

class novo:
    ## Classe oonde forma a primeira janela com os menus
    def __init__(self, janela):
        # Inicia como None

        janela.title("Sapataria do Seu IVO - Menu Inicial")

        self.b1=Button(janela, text='Cadastrar Tenis', command=self.new_janCT)
        self.b1.place(x=335, y=245)

        self.b2=Button(janela, text='Relatório Geral', command=self.new_janRG)
        self.b2.place(x=335, y=285)

        self.b3=Button(janela, text='Realizar Venda', command=self.new_janRV)
        self.b3.place(x=335, y=325)

        self.b4=Button(janela, text='Atualizar preços', command=self.new_janAP)
        self.b4.place(x=335, y=365)

        self.b5=Button(janela, text='Cadastrar Cores', command=self.new_janCC)
        self.b5.place(x=335, y=405)

        self.b6=Button(janela, text='Guarda no TXT', command=self.new_janGT)
        self.b6.place(x=335, y=445)

        self.b7=Button(janela, text='Recupera do TXT', command=self.new_janRT)
        self.b7.place(x=335, y=485)

        self.b8=Button(janela, text='Sair', command=root.destroy)
        self.b8.place(x=335, y=525)

        self.l1=Label(janela, text='raiz!')
        self.l1.place(x=750, y=560)

        screen_width = janela.winfo_screenwidth()
        screen_height = janela.winfo_screenheight()

        width = 800
        height = 600

        x = (screen_width/2)-(width/2)
        y = (screen_height/2)-(height/2)

        self.jan = None
        self.caixa=Frame(janela)
        self.caixa.grid()

########################################################################################

    def new_janCT(self):
        def inserindoDados(self, *args):
            self.listbox.insert(END, self.inserir.get())

        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Cadastro de tenis')
            self.l.grid()

            self.lblistbox = Label(self.jan, text="ESTOQUE DO TIO IVO", bg='lightSteelBlue3', fg='blue').place(x=50, y=60)

            self.listbox = Listbox(self.jan, width=60)
            self.listbox.insert(0,"Criando listbox = 1")

            self.listbox.place(x=100, y=200)

            self.lblistbox1 = Label(self.jan, text="Inserindo dados", bg='lightSteelBlue3', fg='blue').place(x=20, y=10)
            self.inserir = StringVar()
            self.txtlistbox = Entry(self.jan, textvariable=self.inserir, width=47).place(x=120, y=10)
            self.btninserir = Button(self.jan, text='INSERIR',height=1, width=10, command=self.inserindoDados).place(x=325, y=30)

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()

#########################################################################################

    def new_janRG(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Relatório Geral')
            self.l.grid()

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()

#########################################################################################

    def new_janRV(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Realizar Venda')
            self.l.grid()

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()


#########################################################################################

    def new_janAP(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Atualizar preços')
            self.l.grid()

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()


#########################################################################################

    def new_janCC(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Cadastrar Cores')
            self.l.grid()

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()


#########################################################################################

    def new_janGT(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Guarda no TXT')
            self.l.grid()

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()


#########################################################################################

    def new_janRT(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)

            self.l=Label(self.jan, text='Recupera do TXT')
            self.l.grid()

            self.jan.geometry('800x600')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()

#########################################################################################            



    def new_janSA(self):
        #self.janela.destroy()
        sys.exit()
        l1.text['Para Fechar clique no X vermelho acima! ']


#########################################################################################            

    def fecha_jan(self):
        # Seta de novo em None para recriar quando abrir
        self.jan.destroy()
        self.jan = None

root=Tk()

novo(root)
root.geometry('800x600')

root.mainloop()
novo()

Error

Exception in Tkinter callback Traceback (Most recent call last): File "C: Users ecaet Appdata Local Programs Python Python37 lib tkinter__init__.py", line 1705, in call Return self.func(*args)

File "G: python Tkinter main.py", line 110, in new_janCT self.btninserir = Button(self.jan, text='INSERT',height=1, width=10, command=self.insertionDados). place(x=325, y=30)

Attributeerror: 'new' Object has in the'insert' attribute'

2 answers

0


I’m not sure I understand the point, but the problem is the way the inserindoDados is referenced. you can remove the inserindoDados of the scope of new_janCT or remove the self in command command=inserindoDados, but that would lead to another mistake, so I suggest you withdraw the inserindoDados of scope.

def inserindoDados(self, *args):
    self.listbox.insert(END, self.inserir.get())
def new_janCT(self):
    # restante do código sem o inserindoDados

0

The cause of the error is that you have not referenced the inserirDados. What you created was not a class method novo rather a function within the method new_janCT. To solve this problem, just change self.inserindoDados by just inserindoDados.

Now remember that you cannot call the function inserindoDados in other parts of your code, as it exists only within the method new_janCT. If you want to call it within other methods you must instantiate it first.

You can do this by changing the scope you create inserindoDados:

# Se você trocar o escopo, deverá usar o "self" quando for chamar o método sempre.
def inserindoDados(self, *args):
    self.listbox.insert(END, self.inserir.get())
def new_janCT(self):
    # Restante do código...

or else putting this code:

# Dentro do método new_janCT
self.inserindoDados = inserindoDados

Browser other questions tagged

You are not signed in. Login or sign up in order to post.