-2

I want to get a directory with a function and use the obtained path in another function. For example, I'm just giving a simple code sample.

def browse_folder():
    input_folder = input("enter your directory: ")
 
def action():
    print(input_folder)

The thing is: until I call the first function, you can't use the variable from the first function in the second function.

But I can't call the first function first because it's graphical (tkinter) and the user needs to click a button first and specify the path, and then call the function inside the button.

Thanks in advance for your guidance.

I have used 'return' and 'global', but they haven't been effective.

7
  • What you want to do can't be done. Until you call browse_folder, input_folder doesn't exist. you simply must call browse_folder if you need access to input_folder. Commented Jul 3, 2024 at 20:14
  • Let's say you could call a function like that at any moment without the tkinter interface, but how would you obtain the path to the input folder without the user interacting with it? Can you show an example of how it would ideally work? Commented Jul 3, 2024 at 20:18
  • You should use return to return the value obtained in browse_folder() and store it in the calling code. Then pass that stored value as an argument to action(). You say you used tried return but it "wasn't effective". Perhaps you could post the code you tried for that as well, since you were on the right track. Commented Jul 3, 2024 at 20:22
  • something nearer what you actually want and some reasoning behind as others have alluded to. Commented Jul 3, 2024 at 20:39
  • It seems like you could just make input_folder a global variable. Am I missing something? Commented Jul 3, 2024 at 21:43

1 Answer 1

1

does the following help you ? inside the browse function we reference the input_folder as global so the value supplied is visible to the action function

input_folder=""
def browse():
     global input_folder
     input_folder = input('enter directory:')
 
def action():
     print('input_folder:', input_folder )

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

2 Comments

I think that OP is still learning procedural programming so your answer with OOP might be a bit hard for them. Also it would be best to explain step by step what your code does
@TheLizzard, fair enough, I'll replace with a non OOP as that may just add to the OP's confusion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.