Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Don't understand wxPython ids

Reply
Thread Tools

Don't understand wxPython ids

 
 
Grant Edwards
Guest
Posts: n/a
 
      04-27-2004
I've decided to learn wxPython, and I'm afraid I just don't
grok the whole "id" thing where you have to pull unique
integers out of your, er, the air and then use those to refer
to objects:

From whe wxPython wiki examples:

self.button =wxButton(self, 10, "Save", wxPoint(200, 325))
EVT_BUTTON(self, 10, self.OnClick)

Does the 10 have any function other than as a handle that
allows you to refer to self.button in the EVT* call?

You're supposed to just make up unique id numbers for objects
when a) they've already got unique id numbers [at least the id
builtin thinks so] and b) they've got names that make things
even more readable?

This feels very assmebly-level. No, it's even worse than
assembly language, since even assembly language has labels and
symbols.

Why not this:

self.button =wxButton(self, 10, "Save", wxPoint(200, 325))
EVT_BUTTON(self, self.button, self.OnClick)

Or better yet this:

self.button =wxButton(self, 10, "Save", wxPoint(200, 325), action=self.OnClick)

This last way seems pretty intuitive...

Can somebody clue me in on the advantages of the
progrmmer-generated integer id numbers for objects?

--
Grant Edwards grante Yow! Now KEN and BARBIE
at are PERMANENTLY ADDICTED to
visi.com MIND-ALTERING DRUGS...
 
Reply With Quote
 
 
 
 
Greg Krohn
Guest
Posts: n/a
 
      04-28-2004
Grant Edwards wrote:
> I've decided to learn wxPython, and I'm afraid I just don't
> grok the whole "id" thing where you have to pull unique
> integers out of your, er, the air and then use those to refer
> to objects:
>
> From whe wxPython wiki examples:
>
> self.button =wxButton(self, 10, "Save", wxPoint(200, 325))
> EVT_BUTTON(self, 10, self.OnClick)
>
> Does the 10 have any function other than as a handle that
> allows you to refer to self.button in the EVT* call?


AFAIK, yes. But then again, I don't know much. Note that you can use -1
as the id and the button will create it's own unique id for you. Then
all you have to do is call button.GetId() whenever you need it.

> You're supposed to just make up unique id numbers for objects
> when a) they've already got unique id numbers [at least the id
> builtin thinks so] and b) they've got names that make things
> even more readable?
>
> This feels very assmebly-level. No, it's even worse than
> assembly language, since even assembly language has labels and
> symbols.
>
> Why not this:
>
> self.button =wxButton(self, 10, "Save", wxPoint(200, 325))
> EVT_BUTTON(self, self.button, self.OnClick)
>
> Or better yet this:
>
> self.button =wxButton(self, 10, "Save", wxPoint(200, 325), action=self.OnClick)
>
> This last way seems pretty intuitive...
>
> Can somebody clue me in on the advantages of the
> progrmmer-generated integer id numbers for objects?


In the new wxPython (2.5.1.5) there's an easier way to bind events
without using ids at all using widget.Bind, like this:



import wx

class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

self.button = wx.Button(self, -1, "What's my id?")
self.button.Bind(wx.EVT_BUTTON, self.OnButtonPress)

def OnButtonPress(self, event):
self.button.SetLabel("My id is: %d" % self.button.GetId())


app = wx.App()
frame = MyFrame(None, -1, "Events and ids")
frame.Show(True)
app.MainLoop()



I can only think of one reasn why they chose to use ids in the first
place. Assigning many objects the same id allows you to use one EVT_*
call for those objects. But is that really a useful feature? Who knows?

greg
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      04-28-2004
In article <BOEjc.12559$(E-Mail Removed) .net>, Greg Krohn wrote:

> AFAIK, yes. But then again, I don't know much. Note that you can use -1
> as the id and the button will create it's own unique id for you. Then
> all you have to do is call button.GetId() whenever you need it.


Ya know, now that you mention it, I think I actually new that
once a couple years back.

>> Can somebody clue me in on the advantages of the
>> progrmmer-generated integer id numbers for objects?

>
> In the new wxPython (2.5.1.5) there's an easier way to bind events
> without using ids at all using widget.Bind, like this:
>
> import wx
>
> class MyFrame(wx.Frame):
> def __init__(self, *args, **kwargs):
> wx.Frame.__init__(self, *args, **kwargs)
>
> self.button = wx.Button(self, -1, "What's my id?")
> self.button.Bind(wx.EVT_BUTTON, self.OnButtonPress)


That's definitely getting closer to what I would expect in
something designed in the last 15-20 years, though passing the
binding as a parameter to wx.Button() sure seems like the
obvious solution for 90% of the cases I run into. Perhaps I'm
too used to Tk.

> I can only think of one reasn why they chose to use ids in the first
> place. Assigning many objects the same id allows you to use one EVT_*
> call for those objects. But is that really a useful feature? Who knows?


That's about the only thing I could think of. The few
situations where I'd have wanted to do someting like that I'd
gladly put a for-loop iterating over a list of objects in
exchange for being able to use single-line of code the other
90% of the time.

--
Grant Edwards grante Yow! I'm young... I'm
at HEALTHY... I can HIKE
visi.com THRU CAPT GROGAN'S LUMBAR
REGIONS!
 
Reply With Quote
 
Doug Holton
Guest
Posts: n/a
 
      04-28-2004

>>I can only think of one reasn why they chose to use ids in the first
>>place. Assigning many objects the same id allows you to use one EVT_*
>>call for those objects. But is that really a useful feature? Who knows?

>
>
> That's about the only thing I could think of. The few
> situations where I'd have wanted to do someting like that I'd
> gladly put a for-loop iterating over a list of objects in
> exchange for being able to use single-line of code the other
> 90% of the time.


Right, there a couple of special cases to think of when designing event
bindings for a gui toolkit. One is where you want multiple controls to
share the same event handler (like a button and a menu item that do the
same thing). Another case is allowing people to change event bindings
"on the fly", after a window constructor has already been called and the
window set up.

I proposed some changes similar to you, see this thread:
http://lists.wxwidgets.org/cgi-bin/e...kojpgmpdpmio#b
but nothing came out of it. I did code up an implementation for Wax
(http://wiki.wxpython.org/index.cgi/Wax ), but I don't know if it was
included.
 
Reply With Quote
 
Roger Binns
Guest
Posts: n/a
 
      04-28-2004
Greg Krohn wrote:
> I can only think of one reasn why they chose to use ids in the first
> place.


Don't forget how old wxWidgets is, and that it has to interoperate
with the underlying toolkits.

> Assigning many objects the same id allows you to use one EVT_*
> call for those objects. But is that really a useful feature? Who knows?


Here is how I use the same id in multiple locations. I can have a
menu entry, a toolbar button, and a button inside some HTML all
invoke the same function. The only coupling between them is
the id number.

You can also use id ranges in wxPython 2.5 which makes it easy to
send a whole bunch of different items to the same handler.

class FooFrame(wx.Frame):
ID_FILE_DELETE=wx.NewId()

def _init_(...):
...
menu.Append(self.ID_FILE_DELETE, "&Delete", "Delete the file")
....
toolbar.AddLabelTool(self.ID_FILE_DELETE, "Delete", ....)
....
wx.EVT_MENU(self, self.ID_FILE_DELETE, self.OnFileDelete)
....
wx.EVT_BUTTON(self, self.ID_FILE_DELETE, self.OnFileDelete)
....

def OnStateChange(...):
....
self.GetToolBar().EnableTool(self.ID_FILE_DELETE, True)
....

In my HTML:

<wxp class="Button" module="wx">
<param name="label" value="Delete ..."/>
<param name="id" value="gui.FooFrame.ID_FILE_DELETE"/>
</wxp>

Roger


 
Reply With Quote
 
Greg Krohn
Guest
Posts: n/a
 
      04-28-2004
Grant Edwards wrote:
> That's definitely getting closer to what I would expect in
> something designed in the last 15-20 years, though passing the
> binding as a parameter to wx.Button() sure seems like the
> obvious solution for 90% of the cases I run into. Perhaps I'm
> too used to Tk.


I totally agree with you. The Tk way seems much more natural.

>>I can only think of one reasn why they chose to use ids in the first
>>place. Assigning many objects the same id allows you to use one EVT_*
>>call for those objects. But is that really a useful feature? Who knows?

>
>
> That's about the only thing I could think of. The few
> situations where I'd have wanted to do someting like that I'd
> gladly put a for-loop iterating over a list of objects in
> exchange for being able to use single-line of code the other
> 90% of the time.


Ditto. You'd think it wouldn't be so hard to add a kwarg for it. At
least if it's something you can add on the Python side. Hmmm.


greg
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-28-2004
On 2004-04-28, Doug Holton <(E-Mail Removed)> wrote:

>>>I can only think of one reasn why they chose to use ids in the
>>>first place. Assigning many objects the same id allows you to
>>>use one EVT_* call for those objects. But is that really a
>>>useful feature? Who knows?

>>
>> That's about the only thing I could think of. The few
>> situations where I'd have wanted to do someting like that I'd
>> gladly put a for-loop iterating over a list of objects in
>> exchange for being able to use single-line of code the other
>> 90% of the time.

>
> Right, there a couple of special cases to think of when
> designing event bindings for a gui toolkit. One is where you
> want multiple controls to share the same event handler (like a
> button and a menu item that do the same thing).


Just pass the same function to both of them when you create
them?

> Another case is allowing people to change event bindings "on
> the fly", after a window constructor has already been called
> and the window set up.


Provide a method to set the action for an object instance after
it's been created.

Both of these have been SOP in other GUI toolkits for a dog's
age, and I can't figure out any benefit to the ID scheme.
Using an outside function to reach in and torque on the
object's internals seems so un-Pythonesque. But, all sorts of
smart people rave about wxPython, so I thought there must be
some benefit to the ID scheme that I'm missing.

> I proposed some changes similar to you, see this thread:
> http://lists.wxwidgets.org/cgi-bin/e...kojpgmpdpmio#b
> but nothing came out of it. I did code up an implementation
> for Wax (http://wiki.wxpython.org/index.cgi/Wax ), but I don't
> know if it was included.



--
Grant Edwards grante Yow! Nice decor!
at
visi.com
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-28-2004
On 2004-04-28, Roger Binns <(E-Mail Removed)> wrote:

> Here is how I use the same id in multiple locations. I can have a
> menu entry, a toolbar button, and a button inside some HTML all
> invoke the same function. The only coupling between them is
> the id number.
>
> ID_FILE_DELETE=wx.NewId()
> ...
> menu.Append(self.ID_FILE_DELETE, "&Delete", "Delete the file")
> ....
> toolbar.AddLabelTool(self.ID_FILE_DELETE, "Delete", ....)
> ....
> wx.EVT_MENU(self, self.ID_FILE_DELETE, self.OnFileDelete)
> ....
> wx.EVT_BUTTON(self, self.ID_FILE_DELETE, self.OnFileDelete)


I understand, but compare the above code with this:

> menu.Append("&Delete", "Delete the file", action=self.OnFileDelete)
> ....
> toolbar.AddLabelTool("Delete", action=self.OnFileDelete)


Which is easier to understand at a glance?


[pardon the re-arrangement]

> You can also use id ranges in wxPython 2.5 which makes it easy to
> send a whole bunch of different items to the same handler.


Now _that's_ something you can actually point to as a "feature"
of the ID scheme.

--
Grant Edwards grante Yow! .. I want FORTY-TWO
at TRYNEL FLOATATION SYSTEMS
visi.com installed within SIX AND A
HALF HOURS!!!
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-28-2004
On 2004-04-28, Greg Krohn <(E-Mail Removed)> wrote:

>> The few situations where I'd have wanted to do someting like
>> that I'd gladly put a for-loop iterating over a list of
>> objects in exchange for being able to use single-line of code
>> the other 90% of the time.

>
> Ditto. You'd think it wouldn't be so hard to add a kwarg for it. At
> least if it's something you can add on the Python side. Hmmm.


I guess it's simple enough to subclass the widgets I use and
add one, but it feels like going to buy a new car and having to
bring your own steering wheel.

--
Grant Edwards grante Yow! Wow! Look!! A stray
at meatball!! Let's interview
visi.com it!
 
Reply With Quote
 
Roger Binns
Guest
Posts: n/a
 
      04-28-2004
Grant Edwards wrote:
> I understand, but compare the above code with this:
>
> > menu.Append("&Delete", "Delete the file", action=self.OnFileDelete)
> > ....
> > toolbar.AddLabelTool("Delete", action=self.OnFileDelete)

>
> Which is easier to understand at a glance?


If it is all in one place in one function then it is no big deal.
In reality if you have editable toolbars, menus loaded from definitions
elsewhere and stuff scattered across several modules then using
ids makes that easier.

Roger



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Numeric + wxPython, can't understand a bug... John Ladasky Python 2 04-28-2007 11:06 AM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
after transfer of data from MS-outlook(mail ids) to application,mail ids are consisting of strange characters vamsikrishna_b@coolgoose.com Python 2 06-21-2005 12:41 PM
Don't understand wxPython event handling Robert Python 3 04-01-2004 10:46 PM
wxPython looses function "wxPython.wx.miscc" Anand Python 1 07-23-2003 01:59 AM



Advertisments