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.geometry("+%d+%d" % (x,y))

def getString(window,string):
    global 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.


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

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:

Specifically this:

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

such as 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())

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


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:

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.