UnboundLocalError: local variable 'quit_button' referenced before assignment

UnboundLocalError: local variable 'quit_button' referenced before assignment

我正在制作一款能够将主题更改为深色和浅色模式的应用。我疯狂地删除了添加并添加了评论。问题是代码不会更改主题,所以我在函数中创建了用于更改变量 color_mode 全局的函数。但是,当我这样做时,代码中断并按下一个按钮并显示“UnboundLocalError:分配前引用的局部变量'quit_button'”。我不知道该怎么办,我希望能得到一些帮助。对于冗长的解释和冗长的代码,我真的很抱歉,但它是最小的。感谢您的时间、帮助和考虑

注意:

如果我将 quit_button 设为全局变量,它会给出错误“_tkinter.TclError:无法调用“pack”命令:应用程序已被销毁”我可能会制作我所有的按钮和标签global 但我认为必须有一种更简单的方法,因为这会产生另一个需要修复的错误。另一件事,当我将所有放置代码更改为没有全局变量的 .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()

问题是 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"