[Tutor] Tkinter canvas movement question...

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 23 Aug 2001 01:03:07 -0700 (PDT)


On Thu, 23 Aug 2001, Jason O'Dell wrote:

> Fellow list users, I JeSo (long time reader, first time poster) come
> bearing problems... Ok, using Tkinter, I have the following code,
> 
> class dude:
>     def __init__(self):
>         self.physical = screen.create_oval(30,270,60,300, fill='white', outline='white')
>     def left(self):
>         screen.move(self.physical, 5, 5)
> 
> (screen is earlier defined as a Tkinter Canvas widget)
> 
> and elsewhere i have,
> 
> circle = dude()
> root.bind_all('<Left>', circle.left())

Try:

    root.bind_all('<Left>', circle.left)

    (without the parentheses)

    (Hey, that was self-referential advice!  *grin*)


What you had before,

> root.bind_all('<Left>', circle.left())

already calls circle.left() as a function; instead, what we want is just
to pass circle.left off as a function.



Here's a small example to demonstrate the idea:

###
>>> def balloon():
...     print "Boom!"
...
>>> balloon
<function balloon at 0x80e7774>
###


Just as long as we don't put parens at the end, the function doesn't get
called.  It's just a function thing that we can pass around, hot potato
style:

###
>>> basket = [balloon]
>>> story = ('red_riding_hood', basket)
>>> b = story[1][0]
>>> b
<function balloon at 0x80e7774>
>>> b()
Boom!
###

And it's this function itself that we want to pass to bind_all(), and not
the result of calling that function.


Hope this helps!