[Tkinter-discuss] Communicating between objects w/o class and/or global?
John McMonagle
jmcmonagle at velseis.com.au
Tue Jun 23 01:34:43 CEST 2009
> Recently I posted, on this Forum, a msg titled: "Tkinter mouse-
> event w/o class and/or global?" It was successfuly [RESOLVED] .
>
> ow I have related Question:
>
> I have two (c1 & c2) Canvas instances. If I mouse-click on one of the
> canvases (or more likely on a drawn item on the canvas, e.g. on a
> rectangle), I would like to change the background color of the other
> canvas.
>
> Can I do it without classes (OOP) and/or global-variables?
>
Sure can, you just have to pass the widget id as a parameter to the function.
For example:
import Tkinter as tk
root = tk.Tk()
c1 = tk.Canvas(root, bg='green')
c2 = tk.Canvas(root, bg='red')
c1.pack()
c2.pack()
def changeColour(event, c):
if c.cget('bg') == 'red':
c.configure(bg='green')
else:
c.configure(bg='red')
c1.bind('<Button-1>', lambda e, c=c2: changeColour(e, c))
c2.bind('<Button-1>', lambda e, c=c1: changeColour(e, c))
root.mainloop()
Regards,
John
More information about the Tkinter-discuss
mailing list