[wxPython-users] Bind arguments

Matt Webster matt at cudadog.com
Sun Mar 16 15:10:28 PDT 2008


Ben, on the first error I had no luck with including (self, event,  
displayText) in the method definition.  When I did, I would get:
   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)

I think this comes from not knowing how to pass an event and a value  
to the method... which I think you point out in "Second".  I don't yet  
understand how self.OnShow('7') passes a result instead of the method  
as an object but you've given me a path to pursue.  Thanks.

Incidentally, I can also break this by adding a return in the OnShow  
method supporting your assertion in "Second".

with:
     def OnShow(self, displayText) :
         print "SEE:  " + displayText
         return 0

result:
TypeError: Expected callable object or None.


Thanks again Ben,
Matt


thanks for your explanation in "Second".  I've been playing with have  
OnShow
On Mar 16, 2008, at 11:51 AM, Ben Kaplan wrote:
> 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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/20080316/a8c93e5c/attachment.htm


More information about the wxpython-users mailing list