This program on Tkinter does not display its widgets. Can anyone explain why?

Asked

Viewed 38 times

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")

1 answer

0


You were setting/configuring your widgets in your class Autenticar but you weren’t instating it anywhere, I’ve improved the structure of your code a little bit, do it like this:

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 bit of fun with this, and I’ve made a functional example of hitting/missing creds (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()

Browser other questions tagged

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