登录GUI的麻烦

时间:2013-11-20 02:21:35

标签: python-3.x tkinter

每次我运行以下代码时都会收到错误,有人可以帮忙吗? 代码中可能还有其他错误,请帮我修复。

from tkinter import *

class Custom_Button():

    users = [("bobbymartin", "*****"), ("goodday", "*****")]

    def sign_in():
        for(username, password) in users:
            if ent_username.get() == username and password:
                print("Login Successful")

    def Display(Label, Entry, Button, sys):

        root = Tk()
        root.title("Binary Suite")
        lab_username = Label(root, text="Username")
        lab_username.grid(row=0, column=0)
        lab_password = Label(root, text="Password")
        lab_password.grid(row=0, column=0)
        ent_username = Entry(root)
        ent_username.grid(row=0, column=1)
        ent_password = Entry(root)
        ent_password.grid(row=0, column=1)
        but_signin = Button(root, text="Sign In", command=sign_in)
        but_signin.pack(side=LEFT)
        but_quit = Button(root, text="Quit", command=sys.quit)
        but_quit.pack(side=RIGHT)

        root.mainloop()

    Display(Label, Entry, Button, sys)

Custom_Button()

这是我收到的错误。

Traceback (most recent call last):
  File "C:\Users\...\Custom Widgets.py", line 3, in <module>
    class Custom_Button():
  File "C:\Users\...\Custom Widgets.py", line 31, in Custom_Button
    Display(Label, Entry, Button, sys)
  File "C:\Users\...\Custom Widgets.py", line 24, in Display
    but_signin = Button(root, text="Sign In", command=sign_in)
NameError: global name 'sign_in' is not defined

修改:从列表中删除密码

1 个答案:

答案 0 :(得分:1)

sign_in未定义。因为这是一个类定义,所以您需要使用self.sign_in