[wxPython-users] Bind arguments

Ben Kaplan bskaplan14 at yahoo.com
Sun Mar 16 11:51:54 PDT 2008


First, your error:
OnShow takes three arguments in the modified version: self, event, and display text. You are calling that method with only two arguments, self and '7'

Second: the reason what you are doing won't work:
Methods in Python are objects. When you pass "self.OnSeven" to the Bind method, you are giving the method as an object. The event handler then calls self.OnSeven(event) when the event is triggered. When you try calling something like "self.OnShow('7')", you are passing the result of that method, which in this case is None.

This is the only way I can think of to do what you want.

def OnShow(self, displayText) :
        def ReallyShow(self, event) :
            if self.formula:
                self.display.Clear()
                self.formula = False
                self.display.AppendText(displayText)
       return ReallyShow

self.Bind(wx.EVT_BUTTON, self.OnShow('7'), id=1)

----- Original Message ----
From: Matt Webster <matt at cudadog.com>
To: wxPython-users at lists.wxwidgets.org
Sent: Sunday, March 16, 2008 1:53:02 PM
Subject: [wxPython-users]  Bind arguments

Hello. I'm working from tutorial code (http://wiki.wxpython.org/AnotherTutorial 
) building the calculator and came across this:

self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
...
OnSeven, OnEight, and OnNine all have the same code except for what  
they display:
def OnSeven(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('7')

Being lazy and clever, I thought I would change the functions to all  
be the same and just pass a new value:
self.Bind(wx.EVT_BUTTON, self.OnShow('7'), id=1)
self.Bind(wx.EVT_BUTTON, self.OnShow('8'), id=2)
self.Bind(wx.EVT_BUTTON, self.OnShow('9'), id=3)
...
def OnShow(self, event, displayText):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText(displayText)

When I run it, I get the following error:
Traceback (most recent call last):
File "./x.py", line 184, in <module>
app = MyApp(0)
File "/BinaryCache/wxWidgets/wxWidgets-11~57/Root/System/Library/ 
Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wx-2.8-mac- 
unicode/wx/_core.py", line 7757, in __init__
File "/BinaryCache/wxWidgets/wxWidgets-11~57/Root/System/Library/ 
Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wx-2.8-mac- 
unicode/wx/_core.py", line 7354, in _BootstrapApp
File "./x.py", line 179, in OnInit
frame = MyFrame(None, -1, 'calculator.py')
File "./x.py", line 52, in __init__
self.Bind(wx.EVT_BUTTON, self.OnShow('7'), id=1)
TypeError: OnShow() takes exactly 3 arguments (2 given)

After fooling around for a few hours with this, I'm getting to the  
point where I believe the Bind() doesn't allow functions with  
arguments. Every example I've pulled on the web supports this but so  
far I haven't found any docs that say this too.

Ideas? 

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org







      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/20080316/e0816b4e/attachment.htm


More information about the wxpython-users mailing list