This program in Tkinter does not display your widgets. Can someone explain why?

1
from tkinter import *

class Autenticar(object):

 def __init__(self, toplevel):

 self.Lab1 = Label(main, text = "Usuário", fg = "Blue")

 self.Lab1.pack()


        self.Entr1 = Entry(main)
        self.Entr1.pack()


        self.Lab2 = Label(main, text = "Senha", fg = "Blue")
        self.Lab2.pack()


        self.Entr2 = Entry(main)
        self.Entr2.pack()



        self.Bot1 = Button(main, text = "Confirmar", fg = "Black", command = self.AcessoNegado )
        self.Bot1.pack()

        def AcessoNegado(self):
            self.Entr1.get()
            self.Lab1["text"] = "Usuario Invalido"
            self.Lab1["fg"] = "Red"

main = Tk()

main.geometry("250x250")

main.title("Autenticar")
    
asked by anonymous 29.12.2016 / 00:28

1 answer

1

You were defining / configuring your widgets in your class Autenticar but you were not instantiating it anywhere, I improved the structure of your code quite a bit, so do:

from tkinter import *

class Autenticar():

    def __init__(self):

        self.main = Tk()
        self.mount_gui()

    def mount_gui(self):
        self.main.geometry("250x250")
        self.main.title("Autenticar")

        self.Lab1 = Label(self.main, text = "Usuário", fg = "Blue")

        self.Lab1.pack()
        self.Entr1 = Entry(self.main)
        self.Entr1.pack()

        self.Lab2 = Label(self.main, text = "Senha", fg = "Blue")
        self.Lab2.pack()

        self.Entr2 = Entry(self.main)
        self.Entr2.pack()

        self.Bot1 = Button(self.main, text = "Confirmar", fg = "Black", command = self.AcessoNegado )
        self.Bot1.pack()
        self.main.mainloop()

    def AcessoNegado(self):
        self.Entr1.get()
        self.Lab1["text"] = "Usuario Invalido"
        self.Lab1["fg"] = "Red"

Autenticar()

I've been having a little fun with this, and I've made a working example of correct / failed credentials (user: Jefferson_Andr, pass: password):

from tkinter import *

class Autenticar():

    def __init__(self):

        self.main = Tk()
        self.password = 'password'
        self.usuario = 'Jefferson_Andr'
        self.mount_gui()


    def mount_gui(self):

        self.main.geometry("250x250")
        self.main.title("Autenticar")

        self.Lab1 = Label(self.main, text = "Usuário", fg = "Blue")

        self.Lab1.pack()
        self.Entr1 = Entry(self.main)
        self.Entr1.pack()

        self.Lab2 = Label(self.main, text = "Senha", fg = "Blue")
        self.Lab2.pack()

        self.Entr2 = Entry(self.main)
        self.Entr2.pack()

        self.Bot1 = Button(self.main, text = "Confirmar", fg = "Black", command = self.auth )
        self.Bot1.pack()
        self.main.mainloop()

    def auth(self):
        if(self.Entr1.get() != self.usuario or self.Entr2.get() != self.password):
            self.AcessoNegado()
            return None
        self.AcessoPermitido()

    def AcessoPermitido(self):
        self.Entr1.get()
        self.Lab1["text"] = "Olá {}".format(self.usuario)
        self.Lab1["fg"] = "Green"

    def AcessoNegado(self):
        self.Entr1.get()
        self.Lab1["text"] = "Usuario Invalido"
        self.Lab1["fg"] = "Red"

Autenticar()
    
29.12.2016 / 10:35