0

I have problem with returning a variable from custom pop-up, when the pop-up is in different .py file. As already answered on Stack Overflow, this can be solved with global variables. However this solution works only if all functions are in a single .py. Code representing the issue is following:

main.py:

from tkinter import *
import main_popups as main_popups

def main_screen():
    global screen
    screen = Tk()   
    main_btn= Button(screen, text = "Press me")
    main_btn.pack()  
    main_btn.configure(command = pressed)

    screen.mainloop()

def pressed():
    main_popups.input_box(screen)
    print(output_variable)

main_screen()

main_popups.py:

def input_box(screen):
    global input_box_screen
    
    def btn_press():
        global output_variable
        output_variable = entry.get()
        input_box_screen.destroy()
        input_box_screen.update()
        print(output_variable)
        
    input_box_screen = Toplevel(screen)
    
    entry = Entry(input_box_screen)
    entry.pack()   
    btn = Button (input_box_screen, text = "Confirm")
    btn.pack()
    btn.configure(command = btn_press)     
    
    screen.wait_window(input_box_screen)

This raises error at print(output_variable), as output_variable is not returned from btn_press in main_popups.py. My question is how to return the text from Entry in main_popups.py to main.py? I tried to return(output_variable) at the end of def btn_press():, but that does not return anything.

2 Answers 2

1

For your case, you don't need global variable, just return output_variable at the end of main_popups.input_box():

main.py

from tkinter import *
import main_popups

def main_screen():
    global screen
    screen = Tk()
    main_btn= Button(screen, text = "Press me")
    main_btn.pack()
    main_btn.configure(command = pressed)

    screen.mainloop()

def pressed():
    output_variable = main_popups.input_box(screen)
    print(output_variable)

main_screen()

main_popups.py

from tkinter import *

def input_box(screen):
    output_variable = None

    def btn_press():
        nonlocal output_variable
        output_variable = entry.get()
        input_box_screen.destroy()
        input_box_screen.update()

    input_box_screen = Toplevel(screen)

    entry = Entry(input_box_screen)
    entry.pack()
    btn = Button (input_box_screen, text = "Confirm")
    btn.pack()
    btn.configure(command = btn_press)

    screen.wait_window(input_box_screen)

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

Comments

1

You can just import output_variable with from main_popups import output_variable after you executed main_popups.py (between line 14 and 15)

PS.: Maybe you shouldnt have used the tag tkinter, because its a very specific tag and you would have gotten a faster replie.

1 Comment

Both answers works perfectly, however never heard about this kind of approach, thank you as this may come handy in the future.

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.