-1
我創建了一個調用兩個函數的新函數。我試圖做一個登錄系統,首先檢查用戶名和密碼。如果用戶名和密碼正確,我希望它切換到下一頁,即PageOne。同時調用兩個函數
import tkinter as tk
import tkinter.messagebox as tm
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="UserName", font=LARGE_FONT).grid(row=0, sticky="E")
label2 = tk.Label(self, text="Password", font=LARGE_FONT).grid(row=1, sticky="E")
entry = tk.Entry(self).grid(row=0, column=1)
entry2 = tk.Entry(self,show="*").grid(row=1, column=1)
button = tk.Button(self, text="Log in",
command=callboth)
button.grid(columnspan=2)
def login(self, parent, controller):
username = entry.get()
password = entry1.get()
if username == ("A") and password == ("123"):
tm.showinfo("Login info","Welcome Doan")
else:
tm.showerror("Login error","Incorrect username")
def callboth(self, parent, controller):
login()
controller.show_frame(PageTwo)
當我運行程序時,這是我得到的錯誤。
Traceback (most recent call last):
File "H:/A2 Computing/ddd2.py", line 162, in <module>
app = SeaofBTCapp()
File "H:/A2 Computing/ddd2.py", line 24, in __init__
frame = F(container, self)
File "H:/A2 Computing/ddd2.py", line 50, in __init__
command=callboth)
NameError: global name 'callboth' is not defined
'命令= callboth' - >'命令= self.callboth' – falsetru
回調有參數。但他們不會被爭辯。您可能需要在'__init__'中將'parent','controller'保存爲屬性,並在回調中使用該屬性。 – falsetru