2017-10-19 117 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是非常棘手的,因爲您並不總是在控制檯中立即看到回溯,但是如果您退出該窗口,則會看到一個回溯,顯示出您在哪裏搞砸了。