[英]Why getting error : "home_frame is not defined" even if it's already global?
我正在創建一個簡單的 Tkinter GUI 應用程序,它有一個簡單的登錄頁面。 當用戶登錄時,主頁會通過三個按鈕打開:go 到頁面 A,go 到頁面 B 或注銷。 但我收到錯誤:第 75 行,在 show_login_page home_frame.pack_forget() NameError: name 'home_frame' is not defined
即使 home_frame 在第 33 行被聲明為全局的。
這是我的代碼:
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Login")
window.geometry("300x300")
# Create the login page as a frame
login_frame = tk.Frame(window)
def check_login():
# Get the username and password from the form
username = username_entry.get()
password = password_entry.get()
# Check the login credentials
if username == "1" and password == "1":
# If the login is successful, destroy the login page and show the home page
login_frame.pack_forget()
show_home_page()
print("Yo")
else:
# If the login is unsuccessful, show an error message
error_label = tk.Label(login_frame, text="Invalid login. Please try again.")
error_label.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Define the function to show the home page
def show_home_page():
global home_frame
# Create the home page as a frame
home_frame = tk.Frame(window)
home_frame.pack()
try:
page_a_frame.pack_forget()
page_b_frame.pack_forget()
login_frame.pack_forget()
except NameError:
pass
# Create the buttons to navigate to the other pages
logout_button = tk.Button(home_frame, text="Logout", command=show_login_page)
page_a_button = tk.Button(home_frame, text="Page A", command=show_page_a)
page_b_button = tk.Button(home_frame, text="Page B", command=show_page_b)
logout_button.pack()
page_a_button.pack()
page_b_button.pack()
# Define the functions to show the other pages
def show_page_a():
global page_a_frame
home_frame.pack_forget()
page_a_frame = tk.Frame(window)
page_a_frame.pack()
tk.Label(page_a_frame, text="This is page A").pack()
back_button = tk.Button(page_a_frame, text="Back", command=show_home_page)
back_button.pack()
def show_page_b():
global page_b_frame
home_frame.pack_forget()
page_b_frame = tk.Frame(window)
page_b_frame.pack()
tk.Label(page_b_frame, text="This is page B").pack()
back_button = tk.Button(page_b_frame, text="Back", command=show_home_page)
back_button.pack()
# Define the function to show the login page
def show_login_page():
global login_frame
global home_frame
home_frame.pack_forget()
login_frame = tk.Frame(window)
login_frame.pack()
# Create the form for the user to enter their username and password
username_label = tk.Label(login_frame, text="Username:")
global username_entry
username_entry = tk.Entry(login_frame, fg="blue")
password_label = tk.Label(login_frame, text="Password:")
global password_entry
password_entry = tk.Entry(login_frame, show="*")
# Place the form widgets on the login page
username_label.pack()
username_entry.pack()
password_label.pack()
password_entry.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Show the login page
show_login_page()
# Run the main loop
window.mainloop()
我以前從未使用過 tkinter。
但是將下面的行添加到頂部似乎有效。
home_frame = tk.Frame(window)
在第 10 行。
編輯:更多信息-我的理解
據我了解,您僅在show_home_page
function 中設置home_frame
變量。
但是,您在調用home_frame.pack_forget
之前調用show_home_page
。
所以,home_frame 實際上什么都不是。
只給全局變量不會分配任何東西,請參閱下面的測試
def foo(a):
global c
print(a)
len(c)
print(c)
>>> foo('adfa')
adfa
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
NameError: name 'c' is not defined
如果您像我們在您的情況下所做的那樣,在頂部將某些內容分配給 c,它會將其視為某些內容,並且您不會收到not defined error
。
如果您仔細觀察,您會發現您首先調用了 function show_login_page()
,並且您正在嘗試使用尚未真正聲明的全局空間中的變量home_frame
。
您首先在show_home_page()
function 中貼花和分配home_frame
,但在show_login_page()
之后被調用,因此show_login_page()
無法了解home_frame
。
因此,要解決此問題,您必須在分配login_frame
變量的行下方的全局空間中聲明並分配home_frame
。
所以,最終的代碼將是這樣的:
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Login")
window.geometry("300x300")
# Create the login page as a frame
login_frame = tk.Frame(window)
home_frame = tk.Frame(window)
def check_login():
# Get the username and password from the form
username = username_entry.get()
password = password_entry.get()
# Check the login credentials
if username == "1" and password == "1":
# If the login is successful, destroy the login page and show the home page
login_frame.pack_forget()
show_home_page()
print("Yo")
else:
# If the login is unsuccessful, show an error message
error_label = tk.Label(login_frame, text="Invalid login. Please try again.")
error_label.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Define the function to show the home page
def show_home_page():
# Create the home page as a frame
home_frame.pack()
try:
page_a_frame.pack_forget()
page_b_frame.pack_forget()
login_frame.pack_forget()
except NameError:
pass
# Create the buttons to navigate to the other pages
logout_button = tk.Button(home_frame, text="Logout", command=show_login_page)
page_a_button = tk.Button(home_frame, text="Page A", command=show_page_a)
page_b_button = tk.Button(home_frame, text="Page B", command=show_page_b)
logout_button.pack()
page_a_button.pack()
page_b_button.pack()
# Define the functions to show the other pages
def show_page_a():
global page_a_frame
home_frame.pack_forget()
page_a_frame = tk.Frame(window)
page_a_frame.pack()
tk.Label(page_a_frame, text="This is page A").pack()
back_button = tk.Button(page_a_frame, text="Back", command=show_home_page)
back_button.pack()
def show_page_b():
global page_b_frame
home_frame.pack_forget()
page_b_frame = tk.Frame(window)
page_b_frame.pack()
tk.Label(page_b_frame, text="This is page B").pack()
back_button = tk.Button(page_b_frame, text="Back", command=show_home_page)
back_button.pack()
# Define the function to show the login page
def show_login_page():
global login_frame
global home_frame
home_frame.pack_forget()
login_frame = tk.Frame(window)
login_frame.pack()
# Create the form for the user to enter their username and password
username_label = tk.Label(login_frame, text="Username:")
global username_entry
username_entry = tk.Entry(login_frame, fg="blue")
password_label = tk.Label(login_frame, text="Password:")
global password_entry
password_entry = tk.Entry(login_frame, show="*")
# Place the form widgets on the login page
username_label.pack()
username_entry.pack()
password_label.pack()
password_entry.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Show the login page
show_login_page()
# Run the main loop
window.mainloop()
我運行了這個程序..它可以工作,但它仍然有一些你必須注意的布局錯誤。 祝你好運:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.