Doubt about Python methods and classes

Asked

Viewed 100 times

0

I have the Login class and created an instance of it in the Main class, with the instance I am calling the start method, but in the start method when clicking the 'Log in' button the 'show_entry_fields' method is not executed correctly. When running and clicking the Main class I immediately get on the console: 'wrong login' . That is, I don’t know why, but it is running directly 'Else', without me having to click the button.(if is not running). Can anyone help or explain what I did wrong? Follow the code of both classes

from tkinter import *


class Login:

    @staticmethod
    def show_entry_fields(nome, senha):
        if nome == 'admin' and senha == 'admin':
            master.destroy()
        else:
            print('login errado')

    @staticmethod
    def iniciar():
        master = Tk()
        Label(master, text='Usuário').grid(row=0)
        Label(master, text='Senha').grid(row=1)

        e1 = Entry(master)
        e2 = Entry(master)

        e1.grid(row=0, column=1)
        e2.grid(row=1, column=1)

        Button(master, text='Sair', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
        Button(master, text='Logar', command=Login.show_entry_fields(e1.get(), e2.get())).grid(row=3, column=1, sticky=W, pady=4)
        mainloop()

main class:

 from FechaLogin import Login

 g = Login()
 g.iniciar()
  • 2

    After pasting the auqi code, always use the button {} to shoot the code correctly

1 answer

1


First I could notice that your Login class is very messy, has code to set the interface here and there.

The second thing I noticed, and I recommend you not to do is to use import tkinter *, since this makes the code less readable, and can cause problems of namespace.

After recreating, using as a basis what you put from your code the result I arrived at was this:

import tkinter as tk

class InterfaceLogin(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.master = master
        # Cria Botões para Login e Sair
        self.btn_login = tk.Button(self.master, text='Login', command=self.check_login_info)
        self.btn_quit = tk.Button(self.master, text='Sair', command=self.master.destroy)

        # Texto que é exibido antes da caixa, indicando o que deve ser informado
        self.lbl_usuario = tk.Label(self.master, text='Usuario: ')
        self.lbl_senha = tk.Label(self.master, text='Senha: ')

        # Entrada para Login e Senha respectivamente
        self.entry_login = tk.Entry(self.master)
        self.entry_senha = tk.Entry(self.master, show='*')

        # Posiciona os Botões, Entradas de Texto e Labels na janela usando grid
        self.btn_login.grid(row=3, column=0)
        self.btn_quit.grid(row=3, column=1)

        self.lbl_usuario.grid(row=1, column=0)
        self.lbl_senha.grid(row=2, column=0)

        self.entry_login.grid(row=1, column=1)
        self.entry_senha.grid(row=2, column=1)

   # Função responsavel por validar o Login e Senha digitado
   def check_login_info(self):
       # Usa get() para recuperar as informações digitadas pelo usuario
       usuario = self.entry_login.get()
       senha = self.entry_senha.get()

       if (usuario == 'admin') and (senha == 'admin'):
            print('Login realizado com sucesso.')
       else:
            print('Erro ao realizar o login.')


if __name__ == '__main__':
    root = tk.Tk()
    janela = InterfaceLogin(root)
    janela.mainloop()

Browser other questions tagged

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