小部件上丢失了tkinter事件处理程序引用

时间:2016-03-24 14:07:16

标签: python tkinter event-handling

我在类Verification中将事件处理程序放在一个单独的文件verification.py中(试图使代码模块化)。主GUI代码如下,

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        

现在,我希望inst_lbl中的LoginFrame能够configure并根据user_namepwd显示新文字,但{ {1}}未更改文本(未生成错误)。那么如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

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

立即调用Verification.verify_user并将结果分配给按钮命令。您必须传入对函数的引用,而不是函数的结果。

查看类似问题的答案:https://stackoverflow.com/a/5771787/7432