0

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.

3
  • 1
    The filepathforinfo variable's value will only be updated while the mainloop() running and the user clicks the button. See explanation of how GUIs work in [my answer}(stackoverflow.com/a/68928369/355230) to a question of which this is essentially a dup, Commented Aug 25, 2021 at 18:58
  • @martineau Thanks for you answer, I checked your explanation, I understand the problem now. But I dont think I can use a Label in this case, because I dont want to show the text, I want to give to another function. Commented Aug 26, 2021 at 0:20
  • 1
    Anass: You don't need to use a Label — the value of filepathforinfo does not have to be displayed anywhere. You just need to have this other function you want to pass the value to be invoked by some part of your GUI after the get_pdf_file() function has been run (or maybe as part of it running). The print() in your program doesn't work because at that point get_pdf_file() hasn't run yet — this was the main point I was trying to make by referring you to that other question. Commented Aug 26, 2021 at 0:53

1 Answer 1

0

The problem is your printing filepathforinfo before its even changed. I created a label to show it as it actually works.

import tkinter.filedialog
import tkinter as tk

def get_pdf_file():
    global filepathforinfo
    filetypes=[('PDF files', '*.pdf')]
    filepathforinfo = tk.filedialog.askopenfilename(  title='Open a file',initialdir='/', filetypes=filetypes)
    info_label['text'] = filepathforinfo

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)

info_label = tk.Label(Information_Window, text= filepathforinfo)
info_label.grid()

Information_Window.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.