cantabile wrote:
Hi,
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
...
class MainClass:
...
global UI = self
Then,
def OnClik(button):
UI.do_something (button.get_lab el())
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.
import wx
class paramDialog(wx. Dialog):
count = 0 # Class variable counts # of instances
def __init__(self, parent):
wx.Dialog.__ini t__(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
btn.Bind(wx.EVT _BUTTON, self.shutdown)
self.Bind(wx.EV T_CLOSE, self.shutdown)
self.SetAutoLay out(True)
paramDialog.cou nt += 1
def shutdown(self, evt):
paramDialog.cou nt -= 1
self.Destroy()
if paramDialog.cou nt == 0:
app.Destroy()
import sys
sys.exit("Done" )
class MyApp(wx.App):
# wxWidgets calls this method to initialize the application
def OnInit(self):
frame = wx.Frame(None, -1, "This is the main frame")
self.SetTopWind ow(frame)
d1 = paramDialog(fra me)
d2 = paramDialog(fra me)
d3 = paramDialog(fra me)
d1.Show()
d2.Show()
d3.Show()
return True
if __name__ == '__main__':
app = MyApp(0)
app.MainLoop()
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC
http://www.holdenweb.com/