Doubts about Python methods and classes

0

I have the Login class and created an instance of it in the Main class, with the instance I'm calling the start method, however in the start method when clicking the 'Log in' button the method 'show_entry_fields' is not executed correctly. When I run and click the Main class I immediately get to the console: 'wrong login'. That is, I do not know why, but it is executing the 'else' directly, without having to click on the button (the if is not running). Can anyone help or explain what I did wrong? Follow the code for 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()
    
asked by anonymous 22.08.2018 / 00:33

1 answer

1

First I noticed that your Login class is very messy, it has code to define the interface here and there.

Second thing I noticed, and I recommend not to do is to use import tkinter * , since this makes the code less readable, as well as being able to cause problems with namespace .

After re-creating, based on what you put in your code the result I got 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()
    
24.08.2018 / 20:47