Setting the ID for widgets

Hello,

Until now, I put -1 or wx.NewId() in the widgets’ contructors.

Based on some events or buttons, I need to get the IDs and the contents of the widgets to reuse them somewhere else.

How do the automatic setting of the Ids work ?

Can I set an arbitrary Id for each widget (1,2,3,…) and in that case, are there rules I should follow or Id numbers I should avoid ?

Thanks for your ideas

Dominique

Hi,

You can also use something like this:

at the beginning of the file:

ID_SOME_BUTTON = wx.NewId()

and later in the code…

some_button = wx.Button(parent, ID_SOME_BUTTON, label=“Click me! I dare you!”)

But the best way to reuse is to encapsulate the whole functionality and expose only the minimum.
take a look at this series of articles:
http://www.javaworld.com/javaworld/jw-07-1999/jw-07-toolbox_p.html?page=1

they use java but the ideas behind are universal… a good read IMHO.

Peter

···

On 3/30/07, DomDom BestDomDom@neuf.fr wrote:

Hello,

Until now, I put -1 or wx.NewId() in the widgets’ contructors.

Based on some events or buttons, I need to get the IDs and the contents of the widgets to reuse them somewhere else.

How do the automatic setting of the Ids work ?

Can I set an arbitrary Id for each widget (1,2,3,…) and in that case, are there rules I should follow or Id numbers I should avoid ?

Thanks for your ideas

Dominique


There is NO FATE, we are the creators.

In addition to the solutions already posted, you can create the
widgets and then retrieve the ID for use elsewhere.

However, in wxPython, I tend to ignore IDs almost totally and simply
pass the widget itself anywhere I need it.

···

On 3/30/07, DomDom <BestDomDom@neuf.fr> wrote:

Hello,

Until now, I put -1 or wx.NewId() in the widgets' contructors.
Based on some events or buttons, I need to get the IDs and the contents of
the widgets to reuse them somewhere else.
How do the automatic setting of the Ids work ?
Can I set an arbitrary Id for each widget (1,2,3,...) and in that case, are
there rules I should follow or Id numbers I should avoid ?

Thanks for your ideas
Dominique

DomDom wrote:

Until now, I put -1 or wx.NewId() in the widgets' contructors.

I think the cleanest way to do it is to not specify an id at all, and use keyword args for the stuff you do need to specify:

MyButton = wx.Button(parent, label="ALabel")

There are a few places where an id must be specified, wx.Menu.Append() for instance. IN that case, use:

wx.ID_ANY

(which equals -1, but I think it's clearer and more future proof.)

Chris Mellon wrote:

in wxPython, I tend to ignore IDs almost totally and simply
pass the widget itself anywhere I need it.

me too, but if you really do need it:

id = MyButton.GetId()

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov