By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Managing events

P: n/a
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 ?
Sep 3 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/

Sep 3 '05 #2

P: n/a
cantabile wrote in news:43**********************@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/
Sep 3 '05 #3

P: n/a
On Sat, 03 Sep 2005 19:03:33 +0200, cantabile <ca**********@wanadoo.fr>
declaimed the following in comp.lang.python:

No solution, just more questions...
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.
Why is "OnClick" a global? Or maybe I just didn't understand your
usage... Do you have just ONE OnClick being used by multiple buttons, or
does each button have its own OnClick?

Second question: what GUI framework? Tkinter, or something else?
Might affect the answer..

As I recall, this is one of those places where Tkinter would suggest
use of a lambda expression to delay function calling so an argument
specific to the button can be supplied... My book is at work, so I can't
do up an example...
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Sep 3 '05 #4

P: n/a
Thanks to you all for these answers. I'll try these ideas and post back
comments and results.
Sep 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.