Managing events

Steve Holden steve at holdenweb.com
Sat Sep 3 17:02:09 EDT 2005


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_label())
> 
> 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.__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
         btn.Bind(wx.EVT_BUTTON, self.shutdown)
         self.Bind(wx.EVT_CLOSE, self.shutdown)
         self.SetAutoLayout(True)
         paramDialog.count += 1

     def shutdown(self, evt):
         paramDialog.count -= 1
         self.Destroy()
         if paramDialog.count == 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.SetTopWindow(frame)
         d1 = paramDialog(frame)
         d2 = paramDialog(frame)
         d3 = paramDialog(frame)
         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/




More information about the Python-list mailing list