473,883 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Don't understand wxPython ids

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.OnC lick)

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...
Jul 18 '05 #1
19 3439
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.OnC lick)

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.Fram e):
def __init__(self, *args, **kwargs):
wx.Frame.__init __(self, *args, **kwargs)

self.button = wx.Button(self, -1, "What's my id?")
self.button.Bin d(wx.EVT_BUTTON , self.OnButtonPr ess)

def OnButtonPress(s elf, event):
self.button.Set Label("My id is: %d" % self.button.Get Id())
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
Jul 18 '05 #2
In article <BO************ ******@newsread 1.news.pas.eart hlink.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.Fram e):
def __init__(self, *args, **kwargs):
wx.Frame.__init __(self, *args, **kwargs)

self.button = wx.Button(self, -1, "What's my id?")
self.button.Bin d(wx.EVT_BUTTON , self.OnButtonPr ess)


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!
Jul 18 '05 #3
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.
Jul 18 '05 #4
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.Fra me):
ID_FILE_DELETE= wx.NewId()

def _init_(...):
...
menu.Append(sel f.ID_FILE_DELET E, "&Delete", "Delete the file")
....
toolbar.AddLabe lTool(self.ID_F ILE_DELETE, "Delete", ....)
....
wx.EVT_MENU(sel f, self.ID_FILE_DE LETE, self.OnFileDele te)
....
wx.EVT_BUTTON(s elf, self.ID_FILE_DE LETE, self.OnFileDele te)
....

def OnStateChange(. ..):
....
self.GetToolBar ().EnableTool(s elf.ID_FILE_DEL ETE, True)
....

In my HTML:

<wxp class="Button" module="wx">
<param name="label" value="Delete ..."/>
<param name="id" value="gui.FooF rame.ID_FILE_DE LETE"/>
</wxp>

Roger
Jul 18 '05 #5
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
Jul 18 '05 #6
On 2004-04-28, Doug Holton <in****@spam.he re> 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
Jul 18 '05 #7
On 2004-04-28, Roger Binns <ro****@rogerbi nns.com> 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(sel f.ID_FILE_DELET E, "&Delete", "Delete the file")
....
toolbar.AddLabe lTool(self.ID_F ILE_DELETE, "Delete", ....)
....
wx.EVT_MENU(sel f, self.ID_FILE_DE LETE, self.OnFileDele te)
....
wx.EVT_BUTTON(s elf, self.ID_FILE_DE LETE, self.OnFileDele te)
I understand, but compare the above code with this:
menu.Append("&D elete", "Delete the file", action=self.OnF ileDelete)
....
toolbar.AddLabe lTool("Delete", action=self.OnF ileDelete)
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!!!
Jul 18 '05 #8
On 2004-04-28, Greg Krohn <gr**@invalid.i nvalid> 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!
Jul 18 '05 #9
Grant Edwards wrote:
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.


Well, no I meant importing something like this each time (it doesn't
actually work, BTW):
import wx

#Subclass wx.PyEventBinde r to catch all EVT_* args
class MyPyEventBinder (wx.PyEventBind er):
def __init__(self, *args, **kwargs):
# Look for EVT_* args, bind them and del them
for kw in kwargs:
if kw.startswith(' EVT_'):
self.Bind(getat tr(wx, kw), kwargs[kw])
del kwargs[kw]
wx.PyEventBinde r.__init__(self , *args, **kwargs)

#A little behind-the-scenes switcheroo
wx.PyEventBinde r = MyPyEventBinder

if __name__ == '__main__':

class MyFrame(wx.Fram e):
def __init__(self, *args, **kwargs):
wx.Frame.__init __(self, *args, **kwargs)
self.button = wx.Button(self, -1, "What's my id?",
EVT_BUTTON=self .OnButtonPress)

def OnButtonPress(s elf, event):
self.button.Set Label("My id is: %d" % self.button.Get Id())

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

If I can't figure this out, I'll try subclassing individule widgets and
throwing it in a module.

greg
Jul 18 '05 #10

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

Similar topics

303
17887
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
3
2892
by: Robert | last post by:
Hello list, could somebody point me to a good reference about wxPython event handling? I have seen many examples but which one is the best. Waht are the advantages and disadvantages? Can you also have a short look at the example below and give me some comments, please? Example:
21
2396
by: j_mckitrick | last post by:
Just thought I'd play devil's advocate... I tried wxPython briefly, and it had some nice features. It has a huge list of dependencies. I don't like the fonts (but I'd love to know how to get fixed fonts, or at least anything that looks better on a small laptop screen. TkInter seems much easier to use... less effort to do things.
25
3370
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard Python GUI toolkit is that Tkinter was there first." - Guido van Rossum Guess, that answers my question, but isn't "Tkinter was there first" a very bad answer? :) It is kinda ugly too, so I wonder why it can't be replaced? Or maybe another GUI...
1
2580
by: timothy.williams | last post by:
I'm trying to install wxPython 2.5.3.1 using Python 2.3.2 on a Fedora 2 machine. I have python in a non-standard place, but I'm using --prefix with the configure script to point to where I have everything. The make install in $WXDIR seemed to go fine. I have the libxw* libraries in my lib/ directory libwx_base-2.5.so@ libwx_gtk_adv-2.5.so.3.0.0* libwx_base-2.5.so.3@ libwx_gtk_core-2.5.so@
7
1250
by: Alex VanderWoude | last post by:
Is there a way to override a method on a class whose source you cannot change in such a way that you can hook into that method's code? After doing some research, it appears that one way to do such a thing is to create a new (non-class) method, and then assign the new method to the class in question, thus replacing the existing class method. However, I have read vague hints in the documentation that this is not a good thing to do (?). ...
25
4299
by: TPJ | last post by:
GUI's etc: PyGtk on Windows "(...) So if someone develops mainly for X and just wants to make sure that it is not impossible to run on Windows, you can use PyGTK. (...)", July 2nd, 1999 pyGTK on Windows "(...) > can i use pyGTK under > Windows???
8
1680
by: Jia Lu | last post by:
Hi all I am using wxPy 2.6.3.2-2, But when run an application with self.Bind , I got an error that there is no Bind. How can I fix it. thanx
5
2608
by: Thierry | last post by:
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I needed to output unicode datas back from a web service, and could not get back unicode/multibyte text before applying an hack that I don't understand (thank you google) I have realized an wxPython simple application, that takes the input
0
9943
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
10750
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
10858
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,...
0
10419
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7974
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
7134
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
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3237
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.