2017-10-19 10 views
0

私がやっている授業プロジェクトの一環として、ログインページを作成してデータベースを組み込む必要があります。私はdoeを普通の形で使っていますが、これをtkinterを使ってGUIに入れ、それを動作させるために私はデータベース内のレコードを呼び出すページ上の関数を使用しなければなりませんでした。私が経験している問題は、この関数を呼び出すと、何もしません。おそらく、コーディングの単純な間違いが原因ですが、なんらかの理由でそれを理解できません。クラスを機能させることができません

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 1つの引数(自己を)受け取るメソッドを作成する必要があります。

def logUserIn(self): 
    ... 

あなたは、このような

それはおそらく self.controllerあるべき
controller.show_frame(HubScreen) 

など他のいくつかのミスがあります。 TkinterのデバッグTkinterは微妙です。なぜならコンソールにすぐにトレースバックが表示されるとは限らないからです。ウィンドウを終了すると、どこが乱れているかを示すトレースバックが表示されます。

関連する問題