我正在尝试制作一个基本登录表单。我在将值传递给检查用户名和密码的函数时遇到了问题。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
这是我的错误: –
异常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'未定义 –
可能的重复[如何将TKinter与Python登录屏幕?](https://stackoverflow.com/问题/ 28156719/how-can-i-integrate-tkinter-with-python-log-in-screen) –