Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Don't understand wxPython ids

 
 
Greg Krohn
Guest
Posts: n/a
 
      04-28-2004
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.PyEventBinder to catch all EVT_* args
class MyPyEventBinder(wx.PyEventBinder):
def __init__(self, *args, **kwargs):
# Look for EVT_* args, bind them and del them
for kw in kwargs:
if kw.startswith('EVT_'):
self.Bind(getattr(wx, kw), kwargs[kw])
del kwargs[kw]
wx.PyEventBinder.__init__(self, *args, **kwargs)

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

if __name__ == '__main__':

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?",
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()



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

greg
 
Reply With Quote
 
 
 
 
Greg Krohn
Guest
Posts: n/a
 
      04-28-2004
Greg Krohn wrote:
> If I can't figure this out, I'll try subclassing individule widgets and
> throwing it in a module.
>
> greg


Here we go. Use the attached module like this:

#yourmodule.py
import wx
from event_kwargs import *

.... your code ...

self.ok_button = wx.Button(self, -1, "Ok", EVT_BUTTON=self.OnOk)

....more code...


The only testing I've done is in the "if __name__ == '__main__'" part of
the module. I *haven't* tested all the widgets and events, but they
should theoretically work.

greg

#event_kwargs.py
#version: 0.0.1
#author: Greg Krohn

import wx

def _event_wrap(klass):
# Backup the original __init__
klass.__classic_init__ = klass.__init__

# The new __init__ that will deal with EVT_* kwargs
def new__init__(self, *args, **kwargs):
binds = {} # The EVT_* kwargs
new_kwargs = {} # The kwargs minus EVT_* kwargs (to pass to the orig __init__)

# Sort the kwargs into binds or new_kwargs
for kw in kwargs:
if kw.startswith('EVT_'):
binds[kw] = kwargs[kw]
else:
new_kwargs[kw] = kwargs[kw]

# Call the orig, __init__ with the cleaned-up kwargs
kwargs = new_kwargs
self.__classic_init__(*args, **kwargs)

# bind the EVT_* kwargs
for evt in binds:
self.Bind(getattr(wx, evt), binds[evt])

klass.__init__ = new__init__
return klass

#wrap the controls
for obj in dir(wx):
if hasattr(getattr(wx, obj), '__bases__'):
if wx.core.Control in getattr(wx, obj).__bases__:
exec "wx.%s = _event_wrap(wx.%s)" % (obj, obj)

if __name__ == '__main__':

class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)

self.button = wx.Button(self, -1, "What's my id?", EVT_BUTTON=self.OnButtonPress)
self.sizer.Add(self.button, 0, flag=wx.EXPAND)
self.text = wx.TextCtrl(self, -1, style=wx.TE_PROCESS_ENTER , EVT_TEXT_ENTER=self.OnTextEnter)
self.sizer.Add(self.text, 1, flag=wx.EXPAND)


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

def OnTextEnter(self, event):
self.text.SetValue(self.text.GetValue() + '<ENTER>')
self.text.SetInsertionPointEnd()

app = wx.App()
frame = MyFrame(None, -1, "event_kwargs.py")
frame.Show(True)
app.MainLoop()
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      04-29-2004
On 2004-04-28, Greg Krohn <(E-Mail Removed)> wrote:

> Here we go. Use the attached module like this:
>
> #yourmodule.py
> import wx
> from event_kwargs import *
>
> ... your code ...
>
> self.ok_button = wx.Button(self, -1, "Ok", EVT_BUTTON=self.OnOk)
>
> ...more code...


I _like_ it. Very clever!

> The only testing I've done is in the "if __name__ == '__main__'" part of
> the module. I *haven't* tested all the widgets and events, but they
> should theoretically work.


I'll certainly give it a try. Thanks!

--
Grant Edwards grante Yow! I'm in LOVE with
at DON KNOTTS!!
visi.com
 
Reply With Quote
 
Greg Ewing
Guest
Posts: n/a
 
      04-29-2004
Roger Binns 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.


Giving an identifier to a command so that it can be
invoked in multiple ways is a good idea, but it would
be much more Pythonic if a *name* could be used as the
identifier rather than a number.

Use of a number here seems to be entirely the result
of blindly carrying over a feature of the C++-oriented
API into Python.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

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

> Here we go. Use the attached module like this:
>
> #yourmodule.py
> import wx
> from event_kwargs import *


Hmm. I guess it's time to upgrade to 2.5.

--
Grant Edwards grante Yow! I'm in a twist
at contest!! I'm in a
visi.com bathtub! It's on Mars!! I'm
in tip-top condition!
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-29-2004
On 2004-04-29, Grant Edwards <(E-Mail Removed)> wrote:

>> #yourmodule.py
>> import wx
>> from event_kwargs import *

>
> Hmm. I guess it's time to upgrade to 2.5.


I guess not. It appears it would require upgrading so many
other things it would probably take at least a week or two.

--
Grant Edwards grante Yow! I just heard the
at SEVENTIES were over!! And
visi.com I was just getting in touch
with my LEISURE SUIT!!
 
Reply With Quote
 
Matt Brubeck
Guest
Posts: n/a
 
      04-29-2004
Grant Edwards <(E-Mail Removed)> wrote in message news:<408ed938$0$17263$(E-Mail Removed) m>...

> 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?


"Wax" is a wxPython wrapper under development at:

http://zephyrfalcon.org/labs/
http://zephyrfalcon.org/weblog/arch_Wax.html

Wax has a much more Pythonic API than wxPython, and gets rid of the
numeric IDs completely. For example:

b = Button(parent, "Save", onsave)

Alternately:

b = Button(parent, "Save")

# ...

b.OnClick = onsave

Here's a longer snippet from the Wax primer:

def CreateMenu(self):
menubar = MenuBar()

menu1 = Menu(self)
menu1.Append("&New", self.New, "Create a new file")
menu1.Append("&Open", self.Open, "Open a file")
menu1.Append("&Save", self.Save, "Save a file")

menubar.Append(menu1, "&File")

self.SetMenuBar(menubar)
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-29-2004
On 2004-04-29, Matt Brubeck <(E-Mail Removed)> wrote:

> "Wax" is a wxPython wrapper under development at:
>
> http://zephyrfalcon.org/labs/
> http://zephyrfalcon.org/weblog/arch_Wax.html
>
> Wax has a much more Pythonic API than wxPython, and gets rid of the
> numeric IDs completely.


I like it! I'm currently downloading a set of Mandrake 10 CDs
so I can run wxPython 2.5. Hopefully the upgrade won't kill
more than a few days. Once the system upgrade is done, I'll
give Wax a try.

--
Grant Edwards grante Yow! ... I want FORTY-TWO
at TRYNEL FLOATATION SYSTEMS
visi.com installed withinSIX AND A
HALF HOURS!!!
 
Reply With Quote
 
Christopher Barker
Guest
Posts: n/a
 
      04-29-2004
Greg Ewing wrote:

> Use of a number here seems to be entirely the result
> of blindly carrying over a feature of the C++-oriented
> API into Python.


Honestly, that's exactly what it is. wxPython is a wrapper around a VERY
large C++ toolkit. It uses SWIG to create wrappers that pretty much
match the C++ API. This has had two big advantages:

1) Robin Dunn could actually get it working in a relatively short period
of time

2) the docs match as well, so We had a pretty complete (if confusing)
set of docs from the beginning

Note that neither of these has anything to do with what a good Python
API is, but I don't think we'd have wxPython at all if Robin had set out
to make the best API he could.

FWIW, wxPython is slowly growing more "pythonic" wrappers. There are
also a couple of efforts to make another layer between wxPython and the
user:

PythonCard
Wax
MindWrapper

Of these, PythonCard is probably the farthest along.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

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

http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Roger Binns
Guest
Posts: n/a
 
      04-30-2004
Greg Ewing wrote:
> Giving an identifier to a command so that it can be
> invoked in multiple ways is a good idea, but it would
> be much more Pythonic if a *name* could be used as the
> identifier rather than a number.


That is kinda what my code is doing The name just
happens to be a static class member mapped to an integer.

> Use of a number here seems to be entirely the result
> of blindly carrying over a feature of the C++-oriented
> API into Python.


There has been much debate about this on the wxPython lists.
One school of thought is to make wxPython as "Pythonic" as
possible, with its own set of documentation and own
community. The fact that there is a C++ library underneath
would be a minor implementation detail.

A second school of thought (to which I belong) is that wxPython
should stick to the C++ API closely unless changes significantly
improve developer productivity. The means that wxPython
remains a member of the wxWidgets community, the standard
wxWidgets documentation remains the same and appropriate,
and it is easy to translate C++/Perl/other language wxWidgets
programs into wxPython and vice versa even for the people
who do not know the languages.

Of course there is no "right answer" as to what is best,
and ultimately it is up to Robin and his good taste. At
the moment wxPython is largely in the second camp, with
some amount of the former present. Some people have
implemented stuff in the former camp on top of wxPython
(eg wax).

People are of course welcome to criticise wxWidgets, but
as someone who as actually done programs on multiple
different platforms and GUI toolkits, I am very impressed.
I'll listen to people who have actually written something
that runs on several platforms and operating systems first.

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