Home > Software design >  UnboundLocalError: local variable 'file1' referenced before assignment please help to fix
UnboundLocalError: local variable 'file1' referenced before assignment please help to fix

Time:09-27

I need that when the button is clicked, the file_name variable is saved to a file. Please tell me how to fix my code:

window = tk.Tk()
window.title("Open")
window.geometry("600x400")
window.resizable(False, False)

file_name = ""


def openu():
    global file_name
    if file_name == "":
        file_name = tfd.askopenfilename()
        with open("Filen.json", "w") as file1:
            json.dump(file_name, file1, indent=2, ensure_ascii=False)
        os.startfile(file_name)
    else:
        with open("Filen.json", "r") as file1:
            json.load(file1)
        os.startfile(file_name)
    if btn1["text"] == "":
        btn1["text"] = file_name


btn1 =  tk.Button(window, text="", command=openu)
btn1.place(x = 20, y = 25)



window.mainloop()

UPD: When you click on the button, the program opens a dialog box that opens the file. A File.json is created. Everything is displayed, but one thing does not work. I need that when restarting the program, the buttons are not empty. I changed the code, putting the full one.

CodePudding user response:

Here is the code. When the user opens the window, it loads the file path from the json file and automatically sets the button's text. When the user presses the button, it asks for a new file path name. More detailed explanations can be found in the comments in the code.

import tkinter as tk
import tkinter.filedialog as tfd
import json
import os

window = tk.Tk()
window.title("Open")
window.geometry("600x400")
window.resizable(False, False)

file_name = ""

def load_json():
    global file_name

    # Load the json file if it exists
    if os.path.exists("Filen.json"):
        with open("Filen.json") as file1:
            contents = json.load(file1)

            # If the json has a path in it, load the path to file_name
            # Otherwise, set file_name to an empty string ""
            if len(contents) > 0:
                if contents[0] != "":
                    file_name = contents[0]
                else:
                    file_name = ""
            else:
                file_name = ""

    # Create the json file if it does not exist
    else:
        with open("Filen.json", "w") as file1:
            json.dump([file_name], file1, indent=2, ensure_ascii=False)

def openu():
    global file_name

    # Load the json file
    load_json()

    # If file_name is still "", ask the user to input a file path
    path = tfd.askopenfilename()

    # If the user gave a path (did not hit Cancel), save path to file_name
    # and set the button's label
    if path != ():
        file_name = path

        btn1.config(text=file_name)

        # Save file_name to the json file
        with open("Filen.json", "w") as file1:
            json.dump([file_name], file1, indent=2, ensure_ascii=False)

# Load the json file, and put the file name into the button's text
load_json()
btn1 =  tk.Button(window, text=file_name, command=openu)
btn1.place(x = 20, y = 25)

window.mainloop()
  • Related