-1

I put a event handler in a class Verification in a separated file verification.py (tried to make the code modular). The main GUI code is as follows,

from tkinter import *
from make_widget import *
from verification import *

class LoginFrame(Frame):
    def __init__(self, parent):
    super(LoginFrame, self).__init__()

    self.parent = parent        
    self.initUI()

    # initialize the login screen UI  
    def initUI(self): 
        # Set up login frame properties 
        self.parent.title("Login Screen")

        # creating instruction label
        self.inst_lbl = MakeWidget.make_label(self.parent, "Please login to continue")

        # creating labels and entries for user name and password
        self.user_name = MakeWidget.make_entry(self.parent, caption="User Name:")
        self.pwd = MakeWidget.make_entry(self.parent, caption="User Password:", show="*")

        # create a login button
        login_btn = MakeWidget.make_button(self.parent, Verification.verify_user(self.user_name, self.pwd, self.inst_lbl), "Login")        


def main():
    top = Tk()    
    app = LoginFrame(top)
    top.mainloop()


if __name__ == '__main__':
    main()

# verification.py
from tkinter import * 

class Verification:
# verify user name and password
#----------------------------------------------------------------------
@staticmethod
def verify_user(user_name, pwd, inst_lbl):
    """verify users"""
    if user_name.get() == "admin" and pwd.get() == "123":
        inst_lbl.configure(text="User verified")
    else:
        inst_lbl.configure(text="Access denied. Invalid username or password")


# make_widget.py
from tkinter import * 

class MakeWidget(Frame):
def __init__(self, parent):
    super(MakeWidget, self).__init__()

    self.parent = parent        


# create a button widget 
#----------------------------------------------------------------------
@staticmethod
def make_button(parent, command, caption=NONE, side=TOP, width=0, **options):
    """make a button"""
    btn = Button(parent, text=caption, command=command)

    if side is not TOP:
        btn.pack(side=side)
    else:
        btn.pack()    

    return btn


# create a label widget
@staticmethod
def make_label(parent, caption=NONE, side=TOP, **options):
    label = Label(parent, text=caption, **options)

    if side is not TOP:
        label.pack(side=side)
    else:
        label.pack()

    return label


# create a entry widget
@staticmethod
def make_entry(parent, caption=NONE, side=TOP, width=0, **options):
    MakeWidget.make_label(parent, caption, side)
    entry = Entry(parent, **options)
    if width:
        entry.config(width=width)
    if side is not TOP:
        entry.pack(side=side)
    else:
        entry.pack()

    return entry        

Now, I expected the inst_lbl in LoginFrame to be able to configure and display the new text based on the user_name and pwd, but inst_lbl didn't change the text (no error was generated). So how to resolve the issue.

daiyue
  • 7,196
  • 25
  • 82
  • 149
  • 1
    What does "didn't happen" mean? Did it throw an error? If so, what error? Also, this code doesn't run, it has indentation errors and is missing the code for make_label. – Bryan Oakley Mar 24 '16 at 14:19
  • @BryanOakley I have modified the OP, make it a bit clear, added all the code – daiyue Mar 24 '16 at 14:27
  • 1
    indentation is still messed up. And instead of forcing us to save the code to three files, just put all of the classes in one file. – Bryan Oakley Mar 24 '16 at 14:33

1 Answers1

1

The problem is this line:

login_btn = MakeWidget.make_button(self.parent, Verification.verify_user(self.user_name, self.pwd, self.inst_lbl), "Login") 

You are immediately calling Verification.verify_user and assigning the result so the button command. You must pass in a reference to a function, not the result of a function.

See this answer to a similar question: https://stackoverflow.com/a/5771787/7432

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685