-1

I created a small program to select a file and then display the filepath using print OUTSIDE the openfile function (I need to access the file_path variable outside because I plan on using it for other stuff). I declared it as a global variable but the output is still empty. When I use print(file_path) inside the openfile function it works perfectly. I'm new to programming so bear with. TYIW

from tkinter import *
from tkinter import filedialog as fd
import pyperclip

file_path = ''

def OpenFile():
    global file_path
    file_path = fd.askopenfilename()


print(file_path)
#GUI
window = Tk()
window.geometry('600x600')



Open_button = Button(window,text='Select File', command=OpenFile)
Open_button.pack()

window.mainloop()
refrain
  • 51
  • 7
  • you define OpenFile but you don't call it. – erik258 Oct 01 '22 at 14:59
  • `OpenFile` gets called, but it gets called *after* the `print` statement. – Samwise Oct 01 '22 at 15:00
  • Your print statement is after your call to `OpenFile`, put the print statement at the end of the file and see if that makes a difference. – Jeremy Savage Oct 01 '22 at 15:00
  • The `OpenFile` call (possible many of them) happen inside `mainloop()`. If you put the print statement before `mainloop` it will happen before any buttons have clicked; if you put it afterward it will happen right before the app exits. Whatever you want to do with `file_path` needs to be driven by other GUI events. (Also, you should use Tk's mutable variables instead of `global`!) – Samwise Oct 01 '22 at 15:02
  • I tried using a label to print the value of the path and that too doesn't seem to work ( label = Label(window, fg='black', text=file_path, compound='left') label.pack() ) – refrain Oct 01 '22 at 15:23

2 Answers2

1

You are calling the function after you print the value, so it has not yet changed when you print.

Try to print the value after the function is called.

Chris
  • 108
  • 7
  • writing the statement after openfile button command doesn't work either and placing it at the end only prints the path after I close the gui window. – refrain Oct 01 '22 at 15:22
  • Since you are using GUI events, you will have to access the path via like a "Submit" button. Check out information about [the event loop](https://python-course.eu/tkinter/events-and-binds-in-tkinter.php) – Chris Oct 01 '22 at 15:52
1

Python is an interpreter based language, So it read's the code line by line.

file_path = '' # First reads this
 
def OpenFile(): # then reads this function
    global file_path
    file_path = fd.askopenfilename()


print(file_path) # then this

The functions wont execute until you call it. You may have noticed that before printing file_path you havnt called OpenFile function, so the variable file_paths value is ''.

I would to set focus on window.mainloop() function. mainloop is an important part of tkinter application as it tells Python to run Tkinter's Event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.

For more details of mainloop see this answer.

The mainloop checks if any event is been passed, if passed then only updates the tkinter window. mainloop is actually a loop of update() and update_idletask() so it would only execute print(file_path) once unless and untill you have called it multiple times or in another loop.

So there are 3 solution for your problem:

  1. Adding another button and printing through it.
def printing():
    print(file_path)

print_button = Button(window,text='Click to print value:', command=printing)
print_button.pack()
  1. Using text variables:
from tkinter import *
from tkinter import filedialog as fd
import pyperclip

file_path = StringVar()

def OpenFile():
    global file_path
    file_path.set(fd.askopenfilename()) ###

window = Tk()
window.geometry('600x600')



Open_button = Button(window,text='Select File', command=OpenFile)
Open_button.pack()
a=Label(window,textvariable=file_path)
a.pack()

window.mainloop()
  1. Using .config:
def OpenFile():
    global file_path
    file_path = fd.askopenfilename()
    a.config(text=file_path)
Faraaz Kurawle
  • 1,085
  • 6
  • 24
  • Languages are neither compiled nor interpreted; *implementations* choose one of the other to implement the language. CPython *compiles* Python source code to a byte code which is then executed by a virtual machine. Neither approach explains why `OpenFile` is not called immediately after it is *defined*. – chepner Oct 01 '22 at 16:10
  • @chepner , I am not able to get you, the official site of python states *"Python is an interpreted, object-oriented, high-level programming language with dynamic semantics."*. – Faraaz Kurawle Oct 01 '22 at 18:09