Key Binding Problem

Wildman best_lay at yahoo.com
Wed Mar 23 11:40:19 EDT 2016


On Wed, 23 Mar 2016 02:47:47 -0400, Terry Reedy wrote:

> On 3/23/2016 12:28 AM, Wildman via Python-list wrote:
>> On Wed, 23 Mar 2016 03:02:51 +0000, MRAB wrote:
>>
>>> On 2016-03-23 02:46, Wildman via Python-list wrote:
>>>> My question is how do I coax bind into executing the
>>>> button procedures?  Or is there a way to generate the
>>>> button click event from the binding?
>>>>
>>> It won't let you bind to a function called "load_image" because there
>>> isn't a function called "load_image"!
>>>
>>> The "Window" class, however, does have a method with that name.
>>>
>>> Try binding the keys in Window.__init__ or Window.init_window:
>>>
>>>       def init_window(self):
>>>           ...
>>>           root.bind("<l>", self.load_image)
>>
>> Here is what I tried:
>>
>> class Window(tk.Frame):
>>
>>      def __init__(self, master = None):
>>          tk.Frame.__init__(self,master)
>>          self.master = master
>>          root.bind("l", self.load_image)
>>
>> I get this error and it doesn't make any sense to me:
>>
>> Exception in Tkinter callback
>> Traceback (most recent call last):
>>    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__
>>      return self.func(*args)
>> TypeError: load_image() takes exactly 1 argument (2 given)
> 
> Event handlers must have one parameter (other than 'self' for methods), 
> the event, as that is what they will be passed.  You defined load_image 
> like this.
> 
>      def load_image(self):
>          # load image file
> 
> It should be this
> 
>      def load_image(self, event):
>          # load image file
> 
> You are free to ignore the event object and some people then name the 
> parameter '_' (or dummy) to signify that it will be ignored.
> 
>      def load_image(self, _):
>          # load image file
> 
> You must pass the bound method, as you did, and not the function itself 
> (which has two parameters).

Thank you very much.  I have seen sample code that used
'event' in the definition but without any explanation
as to why and without actually using it.  Since my code
seemed to run OK without it, at least until now, I didn't
give it much thought.  My mistake.

I am new to python so understanding comes a little slower
for me.  And I am not a professional programmer.  I enjoy
it as a hobby so my experience is more limited where the
advanced topics are concerned.  Thanks to the knowledgeable
people here I am gaining insight as I go.  Thanks again!

-- 
<Wildman> GNU/Linux user #557453
May the Source be with you.



More information about the Python-list mailing list