I have a class (a gui) with buttons and other controls. A button, for
example, has a callback method, so that writing
b = Button(label, OnClick)
will call the global OnClick method.
Now, if I want the OnClick method to call some of my main class methods,
I need to write:
UI = None
global UI = self
Is this the correct way to do it in Python ? Isn't there a potential
flaw in declaring my MainClass instance as a global variable ?
Yes. Normally graphical widgets are declared as object classes (in both
wxPython and Tkinter, at least) for precisely this reason.
Then the onClick() can be a method of the class, and the callback is a
bound method of the class (in other words a method that's already
identified with a specific instance).
Here's a simple-ish piece of wxPython code to demonstrate. Notice that
each paramDialog closes its own dialog box, because the callback
provided in the event binding is already bound to the instance.
count = 0 # Class variable counts # of instances
def __init__(self, parent):
wx.Dialog.__init__(self, parent, id=-1, title="This is a
Dialog", size=(300, 250))
btn = wx.Button(self, -1, "Close Me", (100, 75))
# THIS LINE ASSOCIATES A BOUND METHOD OF THE CURRENT
# INSTANCE WITH A CLICK ON THE "Close Me" BUTTON OF
# THIS PARTICULAR DIALOG INSTANCE
paramDialog.count += 1
def shutdown(self, evt):
paramDialog.count -= 1
if paramDialog.count == 0:
# wxWidgets calls this method to initialize the application
frame = wx.Frame(None, -1, "This is the main frame")
d1 = paramDialog(frame)
d2 = paramDialog(frame)
d3 = paramDialog(frame)
if __name__ == '__main__':
app = MyApp(0)