I am working on a small project to help me consolidate my learning of python. After endless trial with global variables, and returns; and searching I have decided to post here. Basically when the program is running, the user can select a .txt file from their computer and then the directory is recorded through the findFile() function specifically the my_label variable. I want to use the string stored in my_label to place it in the editFile function specifically the line: my_file = open("File location goes here","a+") in which it would look like my_file = open(my_label,"a+"). If anyone could provide help I would greatly appreciate it.
root = tk.Tk()
canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()
entry1 = tk.Entry (root)
canvas1.create_window(200, 140, window=entry1)
def editFile ():
x1 = entry1.get()
label1 = tk.Label(root, text=x1 )
canvas1.create_window(200, 230, window=label1)
my_file = open("File location goes here","a+")
my_file.read()
my_file.write("\n" + x1)
my_file.close()
def findFile ():
root.filename = filedialog.askopenfilename(initialdir="C:\\Users\\mypc",
title="Select A File", filetypes=(("txt files", "*.txt"),("All Files", "*.*")))
my_label = Label(root, text=root.filename).pack()
canvas1.create_window(200, 290, window=my_label)
button1 = tk.Button(text='Enter Text', command=editFile)
canvas1.create_window(200, 180, window=button1)
button2 = tk.Button(text='Exit', command=root.destroy)
canvas1.create_window(200, 250, window=button2)
button3 = tk.Button(text='Find File', command=findFile)
canvas1.create_window(200, 270, window=button3)
root.mainloop()