Don't understand wxPython ids
Greg Krohn
greg at invalid.invalid
Tue Apr 27 22:38:25 EDT 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
More information about the Python-list
mailing list