473,701 Members | 2,384 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 1301
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
4986
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 when the program is being run on the background. I'm trying to implement a program that would listen...
0
880
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 hover on a link, or open a new browser window when clicking on a link. Doing it using C#, but I...
0
1450
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
4170
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
1086
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
1570
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 application, how could I catch that particular keyboard combination outside of my application.
0
936
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
1732
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 becomes really hard (if not
3
5173
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 content of one of the documents, keystrokes are sent to the handler on its "<body..." tag.
5
2603
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, it doesn't matter if the user presses "Yes" or "No". how do I cancel the FormClosing?
0
8733
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9223
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9083
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8975
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6569
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4410
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3102
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
3
2035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.