[Tkinter-discuss] Tkinter mouse-event w/o class and/or global?
Cameron Laird
Cameron at phaseit.net
Mon Jun 22 14:20:38 CEST 2009
On Mon, Jun 22, 2009 at 04:51:20AM -0700, GKalman wrote:
.
.
.
> As an example:
>
> I want to click on a Canvas and with that change its background color.
>
> The "usual" way is to declare a class (i.e a namespace).
>
> An other way is declare c=Canvas(...) as global.
>
> Question:
>
> Is it possible to do this without a class (OOP) or global variables?
.
.
.
Yes.
I *think* the following exemplifies what you seek:
# Launch this application. Notice the red canvas. Click on it.
import Tkinter
def toggle_color(event):
c = event.widget
if c.cget("bg") == "red":
new_color = "green"
else:
new_color = "red"
c.configure(bg = new_color)
root = Tkinter.Tk()
my_canvas = Tkinter.Canvas(root, bg = "red")
my_canvas.bind("<Button-1>", toggle_color)
my_canvas.pack()
root.mainloop()
More information about the Tkinter-discuss
mailing list