更新
Tkinter回调中的异常回溯(最近一次调用最后一次):文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第1536行,在电话中return self.func(* args)file“tk_cp_successful.py”,第143行,在command = lambda:controller.show_frame(StartPage))
我的错误是
NameError:未定义全局名称“controller”
class LoginPage(tk.Frame):
def __init__(self, parent, controller):
global entry_1
global entry_2
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="test page")
label.pack(pady=20,padx=20)
label_1 = Label(self, text="Username")
label_1.pack()
label_2 = Label(self, text="Password")
label_2.pack()
entry_1 = Entry(self)
entry_1.pack()
entry_2 = Entry(self, show="*")
entry_2.pack()
label_1.grid(row=0, sticky=E)
label_1.pack()
label_2.grid(row=1, sticky=E)
label_2.pack()
entry_1.grid(row=0, column=1)
entry_1.pack()
entry_2.grid(row=1, column=1)
entry_2.pack()
checkbox = Checkbutton(self, text="Keep me logged in")
checkbox.grid(columnspan=2)
checkbox.pack()
logbtn = Button(self, text="Login", command = self.tested)
logbtn.grid(columnspan=2)
logbtn.pack()
def tested(self):
#print("Clicked")
username = entry_1.get()
password = entry_2.get()
#print(username, password)
if username == "test" and password == "test":
#box.showinfo("Login successful", "Welcome test")
button1 = ttk.Button(self, text="Back to new page", command=lambda: controller.show_frame(newPage))
button1.pack()
else:
box.showerror("Login failed", "Incorrect username")