Managing events

C

cantabile

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

Steve Holden

cantabile said:
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
 
R

Rob Williscroft

cantabile wrote in 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.
 
D

Dennis Lee Bieber

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...
--
 
C

cantabile

Thanks to you all for these answers. I'll try these ideas and post back
comments and results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,318
Members
48,002
Latest member
EttaPfeffe

Latest Threads

Top