用于密码验证的条目小部件的Tkinter事件绑定

时间:2016-03-23 14:46:04

标签: python tkinter

我正在尝试使用条目小部件进行密码输入,使用处理函数进行密码验证,使用标签小部件来显示验证结果。到目前为止,代码如下:

class LoginFrame(Frame):
    def __init__(self, parent):
        #Frame.__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
        inst_lbl = self.make_label(self.parent, "Please login to continue")

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

        # create a login button
        login_btn = self.make_button(self.parent, self.verify_user, "Login")        


    # create a button 
    #----------------------------------------------------------------------
    def make_button(self, 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


   def make_label(self, 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


   def make_entry(self, parent, caption=NONE, side=TOP, width=0, **options):
       #Label(parent, text=caption).pack(side=LEFT)
       self.make_label(self.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        


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


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


if __name__ == '__main__':
    main()

现在,我需要通过user_name方法验证pwdverify_user,只要点击按钮inst_lbl,结果就会显示在login_btn中(触发验证)。那么如何将inst_lbl绑定到verify_user以响应login_btn点击事件?以及如何获取user_namepwd的内容以便在verify_user中进行验证?

1 个答案:

答案 0 :(得分:1)

通过添加wp-config.php前缀使它们成为类变量。这样,您可以在创建它们的方法之外访问它们。

self.

尝试呼叫时,您还需要self.inst_lbl = self.make_label(...) self.user_name = self.make_entry(...) self.pwd = self.make_entry(...) 前缀。

self.

除此之外,您在self.inst_lbl.configure(...) 方法中需要self参数,因为它是一个类的成员。

此外,对于Python-2.x,您的verify_user会获得TypeError: must be type, not classobj。您注释掉的行是正确使用的行。

super(LoginFrame, self).__init__()

如果您想使用Frame.__init__(self, parent) ,则应将super()作为参数添加到您的类定义中,因为旧样式类等。

object