473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Managing events

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
4 1214
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
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
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
Thanks to you all for these answers. I'll try these ideas and post back
comments and results.
Sep 4 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: thechaosengine | last post by:
Hi all, Does anyone know of a design pattern to deal with the issue of making UI controls active and inactive according to the context of what the user is doing? I've been thinking about the...
3
by: Torsten Wiebesiek | last post by:
Hi, I am currently working on a computer vision system. At the moment I'm adding support for firewire cameras. Since there is only one firewire system on a computer, I have writen a firewire...
7
by: tgh003 | last post by:
I would be interested to hear how others are managing their javascript (.js) files from the original code vs the obfuscated version they publish to their site/webapp. I currently manage 2 files,...
14
by: RL Stevenson | last post by:
What is a reasonable way to manage a complex form with 5 or so tabs with 100 or more controls bound to 5-10 tables in a database? Pasting all those controls, datasets, data adapters directly onto...
0
by: deepak | last post by:
y objective is to retrieve data from a remote server through sockets.the Maximum no of sockets that can be opened are four. The component that has to be developed in .net which must be COM...
0
by: batista | last post by:
Hello all, Let me first explain my windows application.... I have a grid which is bound to a dataview of a dataset... Now this dataset is updated from a separate thread and therefore I've...
1
by: AJ | last post by:
Hi all, Just throwing something out to the newsgroup to get a feel for what others are doing. I am noticing that with my current coding practice, every time i want to update a database i...
2
by: fariba123 | last post by:
i have designed an employee information site. there is an option to generate pay slip. how can i show the employee related data based on the drop down list. i have found code example for...
6
by: kirk | last post by:
I have three events, using event handler methods as depicted below. Two of those event handler methods need to reset specific data whenever the other event left fires. I wasn't sure how to...
2
by: Justin | last post by:
I've never work with creating controls on the fly. Now that I am, I'm uncertain about a particular aspect. Example: I have a static listbox named ListBox1. I use the following routine: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.