Home > Mobile >  UnboundLocalError: local variable 'quit_button' referenced before assignment
UnboundLocalError: local variable 'quit_button' referenced before assignment

Time:01-22

I am making an app with the ability to change the theme to dark and light mode. I stripped down the add crazily and added comments. The problem is the code won't change the theme so I made the function for changing the variable color_mode global in the function. However, when I do this the code breaks and takes a button and says "UnboundLocalError: local variable 'quit_button' referenced before assignment". I don't know what to do and I was hoping if I could get some help. I'm really sorry for the long explanation and the long code but it was the smallest it could be. Thank you for your time, help, and consideration

NOTE:

If I make quit_button a global variable it gives the error "_tkinter.TclError: can't invoke "pack" command: application has been destroyed" I could potentially make all my buttons and labels global but I think there must be an easier way because this gives another error to fix. Another thing, when I change all the placement code to .place without the global variable it gives the error "_tkinter.TclError: can't invoke "place" command: application has been destroyed" so I'm lost


#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()

CodePudding user response:

The problem was a capitalising typo in the change_color_mode() function. I've corrected it below. You originally set color_mode to "Light" while the conditions in other functions are checking for "light", same with "Dark" and "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"
  •  Tags:  
  • Related