如何使用Tkinter为我的应用程序在python中解决此类型错误?

时间:2017-07-21 11:45:16

标签: python user-interface tkinter types self-reference

每当我使用该程序尝试登录时,我都会收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
  return self.func(*args)
TypeError: LoginCheck() missing 1 required positional argument: 'self'

下面是我的代码片段,我很抱歉它很长但我不知道我需要多少才能准确显示要轻松解决的问题。

class Log(tkinter.Frame):

def __init__(self, parent, controller):
    tkinter.Frame.__init__(self, parent)
    self.userEnt = tkinter.Entry(self, width=50, justify="center", foreground="#000000", background="#FFFFFF")
    self.userEnt.insert(0, "USERNAME")
    self.userEnt.place(relx=0.26, rely=0.346)

    self.passEnt = tkinter.Entry(self, width=50, show="*", justify="center", foreground="#000000", background="#FFFFFF")
    # passEnt.background = "#FFFFFF"
    self.passEnt.insert(0, "PASSWORD")
    self.passEnt.place(relx=0.26, rely=0.413)

    self.logBtn = tkinter.Button(self, text="login", bg="#383A39", fg="#AB97BD", width=15, height=3,
                                 command=Log.LoginCheck)

    self.logBtn.place(relx=0.70, rely=0.346)

    self.createUserBtn = tkinter.Button(self, text="New User", bg="#383A39", fg="#AB97BD", width=15, height=2,
                                   command=lambda: controller.show_frame(NewUser))

    self.createUserBtn.place(relx=0.835, rely=0.925)

def LoginCheck(self):
    global press

    if press >= 1:

        c.execute("SELECT * FROM Users")
        list_of_users = (c.fetchall())
        # print(list_of_users)  Uncomment to print everything in the user table as a list
        position = 0
        for _ in list_of_users:
            user = list_of_users[position]
            if self.userEnt.get() == user[3] and self.passEnt.get() == user[4]:
                print("Welcome,", user[1])
                print("ID:", user[0])
                print("First Name:", user[1])
                print("Last Name:", user[2])
                print("User Name:", user[3])
                print("Password:", user[4])
                app.mainloop()
            elif self.userEnt.get() != user[3] and self.passEnt.get() != user[4]:
                self.userEnt.delete(0, "END")
                self.userEnt.insert(0, "The details entered are the wrong username and password")
            elif self.userEnt.get() != user[3] and self.passEnt.get() == user[4]:
                print("Incorrect username")
                print(user)
                print(press - 1)
                self.userEnt.delete(0, "END")
                self.userEnt.insert(0, "Enter the correct username please. " + str(press - 1) + "/" + str(
                    5) + " tries left")
                press -= 1
            elif self.userEnt.get() == user[3] and self.passEnt.get() != user[4]:
                print("Incorrect password")
                print(user)
                print(press - 1)
                self.userEnt.delete(0, "END")
                self.userEnt.insert(0, "Enter the correct password please. " + str(press - 1) + "/" + str(
                    5) + " tries left")
                press -= 1
            else:
                position += 1

            # The commented code below was the fixed way of logging in via "username" and "password"
            '''
            if userEnt.get() == "username" and passEnt.get() == password:
                menuWindow()
            else:
                print("Incorrect username/password")
                print(press+1) # One is added for readability and easier understanding for debugging
                userEnt.delete(0, END)
                userEnt.insert(0, "Enter the correct details please. " + str(press+1) + "/" + str(5) + " tries left")
                press += 1
            '''

    if press == 1:
        self.userEnt.delete(0, "END")
        self.userEnt.insert(0, "One attempt left. A 20 second ban is imminent.")
    if press == 0:
        self.userEnt.state = "readonly"
        self.passEnt.state = "readonly"
        time.sleep(20)
        press = 5
        self.userEnt.state = "ENABLED"
        self.passEnt.state = "ENABLED"
        self.userEnt.delete(0, "END")
        self.userEnt.insert(0, "Username")

感谢您抽出宝贵时间阅读此内容,请回复竖起大拇指 附:我谈到python是一个菜鸟,我一直在使用教程,但我在一定程度上理解大多数工作是如何工作的。

1 个答案:

答案 0 :(得分:3)

下面:

self.logBtn = tkinter.Button(
   self, text="login", bg="#383A39",  
   fg="#AB97BD", width=15, height=3,
   command=Log.LoginCheck
   )

您正在传递未绑定的方法。未绑定的方法需要明确地将当前实例作为第一个参数传递(因为,在实例上没有调用它们,它们不能在调用时将当前实例作为第一个参数注入)。

您显然想要的是传递self.LoginCheck绑定方法。