How to refer to another widget from within event handler of a button widget

Hi all,

Firstly, really enjoying my first steps in wxPython after quite a bit
of tcl/tk.

I have a whole lot of (identical looking) rows in a grid pattern.
Each row consists of one TextCtrl and one Button.
I have given IDs to each TextCtrl and Button in a simple incrementing
sequence.

I have a bind from each button to a common event handler, which must
get the current
value of the TextCtrl in the same row as the button that was pressed.

I know that I can get the ID of the current button widget by
event.GetId(),
and could then calculate the ID of the corresponding TextCtrl widget.

But how do I reference the TextCtrl get by using it's ID?

Or is there a better way to do this?

Thanks

···

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Hi all,

Firstly, really enjoying my first steps in wxPython after quite a bit
of tcl/tk.
   

welcome!

I have a whole lot of (identical looking) rows in a grid pattern.
Each row consists of one TextCtrl and one Button.
I have given IDs to each TextCtrl and Button in a simple incrementing
sequence.
   

personally, I try to avoid explicit IDs -- they just don't feel "pythonic to me". you may want to take a look at this:

http://wiki.wxpython.org/wxPython%20Style%20Guide

I have a bind from each button to a common event handler, which must
get the current
value of the TextCtrl in the same row as the button that was pressed.

I know that I can get the ID of the current button widget by
event.GetId(),
and could then calculate the ID of the corresponding TextCtrl widget.

But how do I reference the TextCtrl get by using it's ID?
   

wx.FindWindowBy Id() (or somethign like that...)

Or is there a better way to do this?
   

I think so.

The simple way: keep a dict with the IDs as keys and the widgets as values.

What I do for this sort of thing is keep a data structe around that has references to all your widgets. It's structure can make sense for you app -- in this case, for instance, you might have the button and the text control together in a tuple, referenced by the button ID.

Since I'm not fond of IDs, I like to use the "lambda trick" when I want a single event ahndler to handle event s from a bunch of objects:

http://wiki.wxpython.org/Passing Arguments to Callbacks

HTH,

-Chris

···

On 4/27/2010 7:18 AM, dubiboy wrote:

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

This is pretty much exactly how I ended up handling this issue on a recent (my first) project. That was only after running around in circles trying to make other methods work. The cool thing about this method is you don’t need a decoder ring to figure out what is what when you print your dictionary while debugging.

Patrick

···

On Tue, Apr 27, 2010 at 11:52 AM, Chris Barker Chris.Barker@noaa.gov wrote:

The simple way: keep a dict with the IDs as keys and the widgets as values.

Since I’m not fond of IDs, I like to use the “lambda trick” when I want a single event ahndler to handle event s from a bunch of objects:

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Using parent.FindWindowById(id) will restrict the search to the scope of the parent and its children.

···

On 4/27/10 8:52 AM, Chris Barker wrote:

But how do I reference the TextCtrl get by using it's ID?

wx.FindWindowBy Id() (or somethign like that...)

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks Guys,

I tried the dict method just now, it works fine.
I will also look at the partial method.

Again thanks for the fast help.

···

On Apr 27, 9:18 pm, Robin Dunn <ro...@alldunn.com> wrote:

On 4/27/10 8:52 AM, Chris Barker wrote:

>> But how do I reference the TextCtrl get by using it's ID?
> wx.FindWindowBy Id() (or somethign like that...)

Using parent.FindWindowById(id) will restrict the search to the scope of
the parent and its children.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visithttp://groups.google.com/group/wxPython-users?hl=en

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en