I have the following code in Tkinter that uploads 2 files and stores them in variables. I want it so that after each file is chosen, the path of the file is displayed on the Tkinter Window. With the current code, print(file_path.name)
, prints the path however, it's not on the Tkinter Window but rather the console. How can I fix this to print the name in the window itself so that users can see when the run it?
ws = Tk()
ws.title('Uploading Files')
ws.geometry('400x200')
def open_file1():
file_path = askopenfile(mode='r', filetypes=[('All Files', '*.tpi')])
print(file_path.name)
if file_path is not None:
global file1
file1 = file_path.read()
def open_file2():
file_path = askopenfile(mode='r', filetypes=[('All Files', '*.tpi')])
print(file_path.name)
if file_path is not None:
global file2
file2 = file_path.read()
content = []
content2 = []
def uploadFiles(data=None):
global content
content = file1
global content2
content2 = file2
ws.destroy()
first = Label(
ws,
text='Upload old File '
)
first.grid(row=0, column=0, padx=10)
firstbtn = Button(
ws,
text='Choose File',
command=lambda: open_file1()
)
firstbtn.grid(row=0, column=1)
second = Label(
ws,
text='Upload new File '
)
second.grid(row=1, column=0, padx=10)
secondbtn = Button(
ws,
text='Choose File ',
command=lambda: open_file2()
)
secondbtn.grid(row=1, column=1)
upld = Button(
ws,
text='Upload Files',
command=uploadFiles
)
upld.grid(row=3, columnspan=3, pady=10)
ws.mainloop()
CodePudding user response:
update Label inside file dialog
Label(ws, text=file_path.name).grid(row=0, column=10)
def open_file1():
file_path = askopenfile(mode='r', filetypes=[('All Files', '*.png')])
if file_path is not None:
print(file_path.name)
Label(ws, text=file_path.name).grid(row=0, column=10)
global file1
file1 = file_path.read()
I hope it will work for you