0

Hello I'm new to python and I'm stuck I need help.

My problem is this: I have a function that returns a string I call this function in a button How to get this value ?

My code :

from tkinter import* 
from tkinter import filedialog 
file_path = ''
def window(): 
  fen=Tk() fen.geometry('{0}x{1}+{2}+{3}'.format(600, 400, 300, 200)) 
  fen.config(bg = "#87CEEB") 
  return fen 
def select_file(): 
  file_path = filedialog.askopenfilename() 
  return file_path 
def main():  
  fen = window() 
  bouton1 = Button(fen, text = "Select a file", command= select_file()) 
  print(file_path) 
  fen.mainloop() 
main()

If I print the file_path in my functions I can do it, it's perfect,

But I can't get it out of the function

1
  • Ask yourself this: "when does the print statement run, and when does the window function run?" Commented Apr 28, 2022 at 16:11

2 Answers 2

1

You can't get the return value when the function is called as the result of an event (button press, etc). The code that calls the function (inside of mainloop) ignores the return value.

If other parts of the code need the value, you need to store it as an attribute of an object or as a global variable.

The other problem in your revised code is that you're calling the print statement before the user has a chance to click the button, so the value will be blank even if you use a global variable. You have to wait to use the value until after you've clicked the button and selected the file.

For example, create a function that prints the value, and then attach that function to a button. Click your button to choose the file, and then click the button to display the data. As long as you save the value in the first function into a global variable, it will be available in the second function.

Sign up to request clarification or add additional context in comments.

6 Comments

I also tried with a global variable but it does not change anything
@Toniodaninca: then you are doing something wrong. Setting the global variable absolutely will work. Since you didn't provide an example that illustrates the problem it's impossible for me to know what the problem is.
@Toniodaninca: please don't put code in the comments. You can edit your question to add more information.
Yes sorry it's Updated
Even putting the print after the line fen.mainloop() doesn't work
|
0

You wont be able to do this as you are, in theory, returning the file path to the button.

Use the function to set the variable in an object, or set a global variable.

Additionally, you dont need the lambda before your function call, just call the function without the braces (i.e. command = select_file)

2 Comments

Welcome to SO! This is a good answer, but it doesn't really add anything to the question that Bryan's answer didn't already (except the lambda part, which is just a formatting tip). It's a good idea on SO to avoid duplicate answers, since the information in duplicate answers has already been posted, and since some people see them as good targets for downvotes (that's not a threat; just a heads-up). I suggest you read the How to Answer page of the help center if you haven't already, which explains how to answer questions. You can also read the tour for a basic intro to how things work here.
Yes thanks for lambda but with the global it doesn't work either

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.