There are two ways to do it: 1) a global variable 2) to store the variable you want to return in another class:
1)
define the function you use in a button:
def yourFunction (x):
global variab
variab = x
Transfer the function to your button:
button = Button (root, command=lambda: yourFunction (x))
I know that you don't like to use too many globals, so there's a second solution:
- create a class storing the returned variable:
class ClassToStoreReturnedVariable:
def __init__ (self):
self.returnedVariable = None
def returnVariable (self, x):
self.returnedVariable = x
returnedVariable = ClassToStoreReturnedVariable ()
class YourButton:
def __init__ (self):
self.anotherThing = None
self.button = Button (root, command=lambda: returnedVariable.returnVariable (x))
self.pack ()
The button must be written in the end of a button class, because you may want to create a function using self. Such function won't work, if init doesn't create self, before you transfer the function to the button. And don't forget to pack or grid it.