Tkinter dialog and getting result back to calling function

I’ve written a generic function that puts up a dialog with an Entry widget. I am successful in getting the response but now I don’t know how to return that response to the calling method. Here is the dialog function:

def dialog(master,txt,default):
    window=Toplevel()
    window.resizable(False,False)
    string=StringVar()
    string.set(default)
    textLabel=Label(window,text=txt)
    textLabel.grid(row=0,column=0)
    textBox=Entry(window,textvariable=string)
    textBox.grid(row=0,column=1)
    button=Button(window,text='OK',command=partial(getString,window,string))
    button.grid(row=1,column=0,columnspan=2)
    x=master.winfo_x()
    y=master.winfo_y()
    window.geometry("+%d+%d" % (x,y))

def getString(window,string):
    global answer
    answer=string.get()
    window.destroy()
    print(answer)

I call the function like this:

.
.
.    
functions.dialog(self.root,'HTTP Port',self.xml.getHTTPPort())
.
. 
.

How do I get the response back to this call? TIA.

Hi Gw1500se1,

Nowhere in your sample code do you actually call getString so I don’t
think you are actually getting the string at all.

Untested:

Change getString to remove the global line.

Change print(answer) to return answer.

Your dialog function needs to call getString, then return the result
it gets. I think adding return getString() after the last line will be
sufficient.

Then you can say:

myanswer = dialog(...) # Fill in the actual arguments.

If that’s not enough to solve your problem, I think we will need some
more information about how you are calling the dialog function and what
you do next.

You might also like to check out some of these links:

https://duckduckgo.com/?q=how+to+get+result+back+from+tkinter+dialog

Specifically this:

You could also look at the code in the standard library:

such as filedialog.py. But be aware of this:

Thanks, I’ll look through what you posted. I’m not sure what else you need but I want to use the returned value right after the call to ‘dialog’.

from functions import dialog
.
. 
.
functions.dialog(self.root,'HTTP Port',self.xml.getHTTPPort())
print(answer)

As for calling getString, it is in the button definition:

button=Button(window,text=‘OK’,command=partial(getString,window,string))

The print(answer) at the end of ‘getString’ is correct.

Rather than using a global variable, it is better practice to have the
dialog function return a result:

answer = functions.dialog(...)

“As for calling getString, it is in the button definition:
button=Button(window,text=‘OK’,command=partial(getString,window,string))”

Ah, so it is, sorry I’m not used to looking for callbacks in tkinter
functions and I missed that.

Thanks for the reply. That is what I did originally but ‘functions.dialog’ returns immediately rather than waiting for the response. The crux of the problem is I cannot figure out how to ‘wait’ for the dialog to return the result. Is there a tkinter method that blocks until the call back completes? How does that call back return the result to dialog and then the calling function?

Got it. I just found a magical method called wait_window.