2017-10-19 103 views
0

我正在做的课程项目的一部分需要我创建一个登录页面并合并一个数据库,我已经成功地以正常形式使用python,但现在我不得不把它放到一个使用tkinter的图形用户界面中并使其工作我不得不在页面上使用一个函数来调用数据库中的记录,并与用户输入进行比较。我遇到的问题是,当我调用这个函数时,它什么都不做,可能是因为我的代码中有一个简单的错误,但是我不知道是出于某种原因还是出于某种原因。无法在课堂上获得功能

class LogInScreen(tk.Frame): 
    def __init__(self, parent, controller): 

     tk.Frame.__init__(self, parent) 
     description = tk.Label(self, text="Please log in to gain access to the content of this computer science program.", font=LARGE_FONT) 
     description.pack() 

     label1 = tk.Label(self, text="Enter your username:") 
     label1.pack() 

     self.usernameEntry = tk.Entry(self) 
     self.usernameEntry.pack() 

     label2 = tk.Label(self, text="Enter your password:") 
     label2.pack() 

     self.passwordEntry = tk.Entry(self, show="*") 
     self.passwordEntry.pack() 

     notRecognised=tk.Label(self, text="") 

     logInButton = tk.Button(self, text="Log In", command=lambda: self.logUserIn) 
     logInButton.pack() 

     self.controller = controller 

     button1 = tk.Button(self, text="Back to Home", 
        command=lambda: controller.show_frame(SplashScreen)) 
     button1.pack() 

     button2 = tk.Button(self, text="Sign Up", 
        command=lambda: controller.show_frame(SignUpScreen)) 
     button2.pack() 



    def logUserIn(): 
     username = self.usernameEntry.get() 
     password = self.passwordEntry.get() 

     find_user = ("SELECT * FROM user WHERE username == ? AND password == ?") 
     cursor.execute(find_user,[(username),(password)]) 
     results = cursor.fetchall() 

     if results: 
      controller.show_frame(HubScreen) 
     else: 
      loginResult = tk.Label(self, text="Account credentials not recognised, please try again") 
      loginResult.pack() 

我不确知,我应该如何去获得此功能工作,真正需要帮助的人在这里都能够提供的。我花了很长时间看代码并且无所作为,我已经厌倦了解决它,执行程序的其他部分,但是如果没有这个功能,就很难评估它们。很抱歉,对于那些比我更有才华的人来说,这是一个不便之处,但这是一个学习过程,我正在努力改进。

+1

通过'self':'def logUserIn(self):' –

+1

你是怎么称呼它的? – mnistic

+0

为什么你使用一个类登录屏幕? – mrCarnivore

回答

2

在此行中

logInButton = tk.Button(self, text="Log In", command=lambda: self.logUserIn) 

lambda: self.logUserIn 

什么都不做。这定义了一个不带参数的函数,返回函数self.logUserIn。它不会那个函数调用,它只是返回任何self.logUserIn是。换句话说,它实际上是一个无操作。相反,你可以写command=self.logUserIn。但是,你需要做出正确的其中logUserIn需要一个参数(个体经营)的方法:

def logUserIn(self): 
    ... 

你有一些其他的错误有诸如

controller.show_frame(HubScreen) 

这里应该可能是self.controller。调试Tkinter是非常棘手的,因为您并不总是在控制台中立即看到回溯,但是如果您退出该窗口,则会看到一个回溯,显示出您在哪里搞砸了。