Managing events

Rob Williscroft rtw at freenet.co.uk
Sat Sep 3 17:06:03 EDT 2005


cantabile wrote in news:4319d765$0$1738$8fcfb975 at news.wanadoo.fr in 
comp.lang.python:

> 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 ? 

Try:

class MainClass:
  def do_something( self, label ):
    pass

  def OnClick( self, button ):
    self.do_something( button.get_label() )

  def some_method( self ):
    b = Button( label, self.OnClick )


With the above you could also do:

main = MainClass()
b = Button( "A Label", main.OnClick )

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list