python tkinter return value from function used in command

Short answer: you cannot. Callbacks can’t return anything because there’s nowhere to return it to — except the event loop, which doesn’t do anything with return values.

In an event based application, what you typically will do is set an attribute on a class. Or, if you’re a beginner, you can set a global variable. Using a global variable isn’t a good idea for real code that has to be maintained over time but it’s OK for experimentation.

So, for example, since C appears to be a global variable in your example, you would do something like:

def button():
    global C
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    C = A

Leave a Comment