我正在制作一個能夠將主題更改為深色和淺色模式的應用程式。我瘋狂地洗掉了添加并添加了評論。問題是代碼不會改變主題,所以我在函式中創建了用于更改變數 color_mode 全域的函式。但是,當我這樣做時,代碼會中斷并按下按鈕并顯示“UnboundLocalError:分配前參考的區域變數'quit_button'”。我不知道該怎么辦,我希望能得到一些幫助。對于冗長的解釋和冗長的代碼,我真的很抱歉,但它可能是最小的。感謝您的時間、幫助和考慮
筆記:
如果我將 quit_button 設為全域變數,它會給出錯誤“_tkinter.TclError:無法呼叫“pack”命令:應用程式已被破壞”我可能會將所有按鈕和標簽設為全域,但我認為必須有更簡單的方法,因為這給出了另一個需要修復的錯誤。另一件事,當我在沒有全域變數的情況下將所有放置代碼更改為 .place 時,它??會給出錯誤“_tkinter.TclError:無法呼叫“place”命令:應用程式已被破壞”所以我迷路了
解答:如果您收到此錯誤,則可能是大寫錯誤
#Import required libraries
from tkinter import *
#Create an instance of tkinter window
color_mode="dark"
def home():
main_window= Tk()
#Getting height and width of screen
width, height = main_window.winfo_screenwidth(), main_window.winfo_screenheight()
#Define the geometry of the window
main_window.geometry("750x250")
#Making window black for the dark mode to test if we can change the color mode
if color_mode=="dark":
main_window.configure(background="black")
elif color_mode=="Light":
main_window.configure(background="white")
#Making an extra button and function to create the error
def window_destroy():
main_window.destroy()
if color_mode == "dark":
quit_button = Button(main_window, text = "Quit", highlightbackground="black", width=5, font=("Courier New",15), command=window_destroy)
elif color_mode == "light":
quit_button = Button(main_window, text = "Quit", highlightbackground="white", width=5, font=("Courier New",15), command=window_destroy)
quit_button.pack(anchor = "s", side = "right") #The error I get is "UnboundLocalError: local variable 'quit_button' referenced before assignment"
#Make a function for the button
def function():
#Destroying old window
main_window.destroy()
#Creating new window
settings_window = Tk()
settings_window.title("Encryptor")
settings_window.geometry("%dx%d 0 0" % (width,height))
#Making a function to return to the home menu and print color mode
def back():
settings_window.destroy()
home()
print(color_mode)
#Making a button to return to the home menu
if color_mode == "dark":
back_button = Button(settings_window, text="Back", highlightbackground="black", font=("Courier New",15), command=back)
elif color_mode == "light":
back_button = Button(settings_window, text="Back", highlightbackground="white", font=("Courier New",15), command=back)
back_button.pack(anchor="s", side="left")
#Making a listbox and adding elements
color_mode_listbox = Listbox(settings_window, width=30, height=2)
color_mode_listbox.insert("end", "Light mode")
color_mode_listbox.insert("end", "Dark mode")
color_mode_listbox.place(relx=.5, rely=.14, anchor=CENTER)
#Changing color_mode
def change_color_mode ():
global color_mode
if color_mode_listbox.get(ANCHOR) == "Light mode":
color_mode = "Light"
elif color_mode_listbox.get(ANCHOR) == "Dark mode":
color_mode = "Dark"
#Making button to change color_mode
if color_mode == "dark":
select_color_mode_button = Button(settings_window, text="Select", highlightbackground="black", width=10, font=("Courier New",15), command=change_color_mode)
elif color_mode == "light":
select_color_mode_button = Button(settings_window, text="Select", highlightbackground="white", width=10, font=("Courier New",15), command=change_color_mode)
select_color_mode_button.place(relx=.5, rely=.175, anchor=CENTER)
#Running the window
settings_window.mainloop()
#Create a button with highlighted background
main_button = Button(main_window, text="Settings", highlightbackground="black", font=("Courier New",25), command=function)
main_button.place(relx=.5, rely=.5, anchor=CENTER)
#Run the window
main_window.mainloop()
home()
uj5u.com熱心網友回復:
問題是change_color_mode()函式中的大寫拼寫錯誤。我在下面更正了。您最初設定color_mode為“Light”,而其他功能中的條件正在檢查“light”,與“Dark”和“dark”相同。
def change_color_mode ():
global color_mode
if color_mode_listbox.get(ANCHOR) == "Light mode":
color_mode = "light"
elif color_mode_listbox.get(ANCHOR) == "Dark mode":
color_mode = "dark"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419067.html
標籤:
