473,572 Members | 3,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Catching wx events

Hello all,

I am new to this group (and new to Python) and was hoping someone would
be able to help me with something, it is not so much a problem it is
more of a general interest query about something i have a solution too
but am not sure it is the correct one.

I have a class that contains a string ID and a name, and a list
containing a few objects of this type, i need to loop through this list
and create a button for each object (with the name as the label) i have
done this with the following code -

for chan in self._channelli st:
channelbutton = wx.Button(self, id=-1,
label=chan.getN ame())

channelbutton.B ind(wx.EVT_BUTT ON,self._channe lChanged)

My question is this - in the _channelChanged method, how do i know
which button has been pressed when i enter the channel changed method,
and so how do i retrieve the appropriate object depending on which
button has been pressed?

I have potentially solved this problem using the UserData property in a
sizer, i have added all the buttons to a sizer for display purposes and
so SizerItem objects have been created, i can then set the original
object to the UserData object by putting the following line of code in
the loop

sizeritem =
self.topsizer.A dd(channelbutto n,0,wx.ALIGN_RI GHT, userData=chan)

This way i can retrieve the item in the _channelChanged method with the
following -

def _channelChanged (self, event):
eventobj = event.GetEventO bject()
chan = self.topsizer.G etItem(eventobj ).GetUserData()
This works fine but by looking at the API it would appear the UserData
property is not really designed for this use ("userData - Allows an
extra object to be attached to the sizer item, for use in derived
classes when sizing information is more complex than the proportion and
flag will allow for").

Another option would be to derive my own Button class and include the
object in there.

Any advice on the best way to solve this problem would be appreciated.

Many thanks

Ian

Jan 18 '07 #1
2 1294
On 18 Jan 2007 06:12:17 -0800, Cruelemort <ia********@gma il.comwrote:
Hello all,

I am new to this group (and new to Python) and was hoping someone would
be able to help me with something, it is not so much a problem it is
more of a general interest query about something i have a solution too
but am not sure it is the correct one.

I have a class that contains a string ID and a name, and a list
containing a few objects of this type, i need to loop through this list
and create a button for each object (with the name as the label) i have
done this with the following code -

for chan in self._channelli st:
channelbutton = wx.Button(self, id=-1,
label=chan.getN ame())

channelbutton.B ind(wx.EVT_BUTT ON,self._channe lChanged)

My question is this - in the _channelChanged method, how do i know
which button has been pressed when i enter the channel changed method,
and so how do i retrieve the appropriate object depending on which
button has been pressed?

I have potentially solved this problem using the UserData property in a
sizer, i have added all the buttons to a sizer for display purposes and
so SizerItem objects have been created, i can then set the original
object to the UserData object by putting the following line of code in
the loop

sizeritem =
self.topsizer.A dd(channelbutto n,0,wx.ALIGN_RI GHT, userData=chan)

This way i can retrieve the item in the _channelChanged method with the
following -

def _channelChanged (self, event):
eventobj = event.GetEventO bject()
chan = self.topsizer.G etItem(eventobj ).GetUserData()
This works fine but by looking at the API it would appear the UserData
property is not really designed for this use ("userData - Allows an
extra object to be attached to the sizer item, for use in derived
classes when sizing information is more complex than the proportion and
flag will allow for").

Another option would be to derive my own Button class and include the
object in there.

Any advice on the best way to solve this problem would be appreciated.
Exactly how I would do it depends on the rest of the application. I
would probably derive my own button - never be afraid to subclass.

You could also generate the buttons IDs up front, and maintain a
mapping between the IDs and the channels, like so:

self.mapper = {}
for channel in self.channels:
id = wx.NewId()
self.mapper[id] = channel
channelbutton = wx.Button(self, id=id, label=channel.g etName())
def channelChanged( self, event):
channel = self.mapper[event.Id]
You could also use closures (lambdas or via a factory function) to
bind the channel at the time you create the button:

for channel in self.channels:
channelbutton = wx.Button(self, label=channel.g etName())
self.Bind(wx.EV T_BUTTON, lambda event:
self.channelCha nged(channel), source=channelb utton)

def channelChanged( self, channel):
print "Channel changed to ", channel.getName ()
Jan 18 '07 #2

Chris Mellon wrote:
On 18 Jan 2007 06:12:17 -0800, Cruelemort <ia********@gma il.comwrote:
Hello all,

I am new to this group (and new to Python) and was hoping someone would
be able to help me with something, it is not so much a problem it is
more of a general interest query about something i have a solution too
but am not sure it is the correct one.

I have a class that contains a string ID and a name, and a list
containing a few objects of this type, i need to loop through this list
and create a button for each object (with the name as the label) i have
done this with the following code -

for chan in self._channelli st:
channelbutton = wx.Button(self, id=-1,
label=chan.getN ame())

channelbutton.B ind(wx.EVT_BUTT ON,self._channe lChanged)

My question is this - in the _channelChanged method, how do i know
which button has been pressed when i enter the channel changed method,
and so how do i retrieve the appropriate object depending on which
button has been pressed?

I have potentially solved this problem using the UserData property in a
sizer, i have added all the buttons to a sizer for display purposes and
so SizerItem objects have been created, i can then set the original
object to the UserData object by putting the following line of code in
the loop

sizeritem =
self.topsizer.A dd(channelbutto n,0,wx.ALIGN_RI GHT, userData=chan)

This way i can retrieve the item in the _channelChanged method with the
following -

def _channelChanged (self, event):
eventobj = event.GetEventO bject()
chan = self.topsizer.G etItem(eventobj ).GetUserData()
This works fine but by looking at the API it would appear the UserData
property is not really designed for this use ("userData - Allows an
extra object to be attached to the sizer item, for use in derived
classes when sizing information is more complex than the proportion and
flag will allow for").

Another option would be to derive my own Button class and include the
object in there.

Any advice on the best way to solve this problem would be appreciated.

Exactly how I would do it depends on the rest of the application. I
would probably derive my own button - never be afraid to subclass.

You could also generate the buttons IDs up front, and maintain a
mapping between the IDs and the channels, like so:

self.mapper = {}
for channel in self.channels:
id = wx.NewId()
self.mapper[id] = channel
channelbutton = wx.Button(self, id=id, label=channel.g etName())
def channelChanged( self, event):
channel = self.mapper[event.Id]
You could also use closures (lambdas or via a factory function) to
bind the channel at the time you create the button:

for channel in self.channels:
channelbutton = wx.Button(self, label=channel.g etName())
self.Bind(wx.EV T_BUTTON, lambda event:
self.channelCha nged(channel), source=channelb utton)

def channelChanged( self, channel):
print "Channel changed to ", channel.getName ()
Two good ideas, i used the mapping system, works and seems like a
slightly more elegant way of doing things.

Many thanks!

Ian

Jan 18 '07 #3

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

Similar topics

2
4973
by: Olli Piepponen | last post by:
Hi, I'm having a little problem catching keystrokes under Windows. I did a little research and found that with mscvrt.getch() one can cath a single key that is pressed. However this doesn't work when the program is run on the backround and not as the primary task. What I would like to have is a same sort of function that would also work...
0
874
by: Ayende Rahien | last post by:
How do I catch events from a web browser hosted in my application? I host it by adding a reference to SHDocVw and then adding it to my form. Right now I can tell it to fetch a page using Navigate2() and show my html using (IPersistStreamInit)->Load(). I want to know how I hock to events that are happening, so I can show on my status bar when...
0
1442
by: spammy | last post by:
Hi all, I have a windows form that uses an imported COM class to talk to a legacy system. The COM object works asynchronously - An event is fired when data has been returned. This works fine, except that Im getting too many events fired! Thus my windows form becomes pretty sluggish and unresponsive, and CPU utilistaion is pretty high.
9
4161
by: vijay21 | last post by:
hi all, This is about javascript window events. I have a simple html file like this <html> <head> </head> <body onmousedown="alert()"> <form> <input type="text">
0
1083
by: Jeffrey | last post by:
hello, does somebody know how ho to catch GUI events from other processes? GUI events like these 3 for example DoubleClick Move Resize .....
3
1562
by: Billy Porter | last post by:
How would I go about catching events outside my application? Example: Babylon Pro is a program that runs silently (with a hidden GUI) in the system tray. Whenever you use the keyboard/mouse combination CTRL+Right mouse click (in ANY program), the GUI is shown containing the dictionary lookup of the marked word. If I were to write such an...
0
934
by: Lars Netzel | last post by:
Is there any events for catching a ValueChange in a DataView or a Dataset? I looked in the help and I could'nt find anything that I understood how to use, and there was not that many events on those Objects. /Lars
2
1723
by: Anders Borum | last post by:
Hello! I was looking at marking objects with a changed state, once properties have been changed. The reason behind this is, that I would like to enlist such objects for processing in a transaction. The example below illustrates a sample pattern, which is fine when exposing simple value types. However, when exposing reference types, it...
3
5157
by: Robert Inder | last post by:
I am struggling to catch kestrokes within an Internet Explorer 6 window. My window happens to be displaying three frames, though I suspect a similar problem would arise with a single document. The "<body.." tag in each frame includes an "onKeyPress" handler to catch and act on key presses. And if I focus the window by clicking on the...
5
2597
by: lord.zoltar | last post by:
How can I prevent the big close button in the top of the window from closing the window? I want to have and "are you sure?" confirmation so the user must press "Yes" before the program ends. Right now, I've tried catching the FormClosing and FormClosed events. The message box appears at the right time, but since the form is already closing,...
0
7640
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7957
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3685
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.