2016-01-21 88 views
2

我正在嘗試製作一個基本登錄表單。我在將值傳遞給檢查用戶名和密碼的函數時遇到了問題。Python-Tkinter-Basic登錄表單

這裏是我的代碼:

from tkinter import * 

class Application(Frame): 
    def __init__(self,master): 
     super(Application, self).__init__(master)#Set __init__ to the master class 
     self.grid() 
     self.create_main()#Creates function 

    def create_main(self): 
     print("testing") 
     self.title = Label(self, text=" Stuck In The Circle ")#TITLE 
     self.title.grid(row=0, column=2) 

     self.user_entry_label = Label(self, text="Username: ")#USERNAME LABEL 
     self.user_entry_label.grid(row=1, column=1) 

     self.user_entry = Entry(self)      #USERNAME ENTRY BOX 
     self.user_entry.grid(row=1, column=2) 

     self.pass_entry_label = Label(self, text="Password: ")#PASSWORD LABEL 
     self.pass_entry_label.grid(row=2, column=1) 

     self.pass_entry = Entry(self)      #PASSWORD ENTRY BOX 
     self.pass_entry.grid(row=2, column=2) 

     self.sign_in_butt = Button(self, text="Sign In",command = self.logging_in)#SIGN IN BUTTON 
     self.sign_in_butt.grid(row=5, column=2) 

    def logging_in(self): 
     print("hi") 
     user_get = user_entry.get()#Retrieve Username 
     pass_get = pass_entry.get()#Retrieve Password 

     if user_get == 'sam': 
      if pass_get == '123': 
       print("Welcome!") 






#Main 
root = Tk() 
root.title("Stuck in the Circle") 
root.geometry("400x100") 

app = Application(root)#The frame is inside the widgit 
root.mainloop()#Keeps the window open/running 

這是我的錯誤:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Sam\AppData\Local\Programs\Python\Python35-32\lib\tkinter_init_.py", line 1549, in call return self.func(*args) File "C:/Users/Sam/Desktop/Computer Science/Python/Python- Modules/Tkinter/Tkinter Projects/Login Form GUI.py", line 31, in logging_in user_get = user_entry.get()#Retrieve Username NameError: name 'user_entry' is not defined

+0

這是我的錯誤: –

+0

異常Tkinter回調 回溯(最近呼叫最後): 文件「C:\ Users \ Sam \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ tkinter \ __ init__.py」 ,第1549行,__call__ return self.func(* args) 文件「C:/ Users/Sam/Desktop/Computer Science/Python/Python- Modules/Tkinter/Tkinter Projects/Login Form GUI.py」,第31行,在logging_in user_get = user_entry.get()#取回用戶名 NameError:名稱'user_entry'未定義 –

+0

可能的重複[如何將TKinter與Python登錄屏幕?](https://stackoverflow.com/問題/ 28156719/how-can-i-integrate-tkinter-with-python-log-in-screen) –

回答

3
def logging_in(self): 
    print("hi") 
    user_get = self.user_entry.get()#Retrieve Username 
    pass_get = self.pass_entry.get()#Retrieve Password 

使用self.user_entry.get()

當你調用類變量,你需要調用它self.variable_name

+0

非常感謝你!我一直在這個工作了幾個小時,我覺得很愚蠢!代碼全部按照我想要的方式工作。我非常感謝你的幫助:D祝你有美好的一天! –

+0

沒問題:)當你有機會時請選擇最好的答案! –