[Tkinter-discuss] Communicating between objects w/o class and/or global?

Cameron Laird Cameron at phaseit.net
Tue Jun 23 01:32:56 CEST 2009


On Mon, Jun 22, 2009 at 12:00:15PM -0700, GKalman wrote:
			.
			.
			.
> 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?
			.
			.
			.
Like the following?
    
    import Tkinter
    
    def toggle_color(c):
        if c.cget("bg") == "red":
            new_color = "green"
        else:
            new_color = "red"
        c.configure(bg = new_color)
    
    root = Tkinter.Tk()
    c1 = Tkinter.Canvas(root, bg = "red")
    c2 = Tkinter.Canvas(root, bg = "red")
    c1.bind("<Button-1>", lambda event: toggle_color(c2))
    c2.bind("<Button-1>", lambda event: toggle_color(c1))
    c1.pack()
    c2.pack()
    root.mainloop()


More information about the Tkinter-discuss mailing list