1

I am starting out using Canvas objects in Python. I've created the following simple job: the intention is that a blue triangle which, when double clicked, turns yellow. Instead, it is yellow right from the start. What am I doing wrong?

from Tkinter import *

def Yellow():
    canv.itemconfigure(obj,fill='yellow')

root=Tk()
canv=Canvas(root,width=200,height=200)
obj=canv.create_polygon(100,100,120,120,120,80,fill='blue')
canv.tag_bind(obj,'<Double-1>',Yellow())
canv.pack()
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Daz Voz
  • 11
  • 1
  • 2

1 Answers1

3

In the line of code

canv.tag_bind(obj,'<Double-1>',Yellow())

The expression Yellow() calls the function called Yellow. In order to simply refer to a function (say to bind it to an event) instead of calling it, you should just write Yellow. So your code should instead read

canv.tag_bind(obj,'<Double-1>',Yellow)
murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • Thanks very much murgatroid. When I make that change, the triangle does start blue. But now when I double click on it, I get the error: Traceback (most recent call last): File "/usr/lib64/python2.4/lib-tk/Tkinter.py", line 1345, in __call__ return self.func(*args) TypeError: Yellow() takes no arguments (1 given) Seems odd ... I don't _seem_ to have passed any arguments! – Daz Voz Jul 19 '13 at 09:30
  • It looks like tkinter passes an event argument to event handlers, so you want to change your definition of `Yellow` to `def Yellow(event):` (keeping the rest the same). As a side note, in python it is standard practice to lowercase the names of functions and CamelCase the names of classes – murgatroid99 Jul 19 '13 at 14:57
  • @DazVoz by the way, if this answer solved your problem, you should click the check mark to indicate that. – murgatroid99 Jul 19 '13 at 20:31