I'm using tkinter. And I am trying to get a file path from a button and use it as an input for an other function. I defined filepathforinfo as a global variable, but I am not getting the expected result.
I tried to follow the solution here.
Here is the main part of the code:
import tkinter.filedialog
import tkinter as tk
def get_pdf_file():
global filepathforinfo #can be avoided by using classes
filetypes=[('PDF files', '*.pdf')]
filepathforinfo = tk.filedialog.askopenfilename( title='Open a file',initialdir='/', filetypes=filetypes)
filepathforinfo='test'
# Toplevel object which will
# be treated as a new window
Information_Window = tk.Tk()
Information_Window.title("Information extraction")
Information_Window.resizable(True, True)
Information_Window['background']='#8c52ff'
info_button = tk.Button(Information_Window, text="Give the pdf file to extract information from", activebackground='purple',activeforeground='purple' ,command=get_pdf_file)
info_button.grid(row=0,column=0)
print(filepathforinfo)
#get_pdf_info(filepathforinfo)
Information_Window.mainloop()
After clicking the button and choosing the file, I get as an output only this :
Out[1]: 'test'
I dont know why I dont get the file path.
The closest answer I got is here, but it is still not satisfying.