change color

Discussion in 'Python' started by Ben Bush, Dec 5, 2005.

  1. Ben Bush

    Ben Bush Guest

    I tested the following code and wanted to make oval 2 become red after
    I hit the enter key but though the code did not report error, it did
    not change.
    from Tkinter import *
    root=Tk()
    canvas=Canvas(root,width=100,height=100)
    canvas.pack()
    canvas.create_oval(10,10,20,20,tags='oval1',fill='blue')
    canvas.create_oval(50,50,80,80,tags='oval2',fill='yellow')
    def turnRed(self, event):
    event.widget["activeforeground"] = "red"
    self.button.bind("<Enter>", self.turnRed)
    canvas.tag_bind('oval2',func=turnRed)
    root.mainloop()
    Ben Bush, Dec 5, 2005
    #1
    1. Advertising

  2. Ben Bush

    Peter Otten Guest

    Ben Bush wrote:

    > I tested the following code and wanted to make oval 2 become red after
    > I hit the enter key but though the code did not report error, it did
    > not change.
    > from Tkinter import *
    > root=Tk()
    > canvas=Canvas(root,width=100,height=100)
    > canvas.pack()
    > canvas.create_oval(10,10,20,20,tags='oval1',fill='blue')
    > canvas.create_oval(50,50,80,80,tags='oval2',fill='yellow')
    > def turnRed(self, event):
    > event.widget["activeforeground"] = "red"
    > self.button.bind("<Enter>", self.turnRed)
    > canvas.tag_bind('oval2',func=turnRed)
    > root.mainloop()


    The <Enter> event is triggered when you enter a shape with the mouse
    pointer, not when you press the key.
    It seems you cannot associate keypress events with shapes, only with the
    whole canvas.

    Here is some code for you to play with.

    import Tkinter

    def turnRed(event):
    canvas.itemconfigure("oval2", fill="red")

    def turnYellow(event):
    canvas.itemconfigure("oval2", fill="yellow")

    def keypress(event):
    print "you pressed return or enter"
    if "oval2" in canvas.gettags("current"):
    canvas.itemconfigure("current", fill="green")

    root = Tkinter.Tk()

    canvas = Tkinter.Canvas(root, width=100, height=100)
    canvas.pack()
    canvas.create_oval(10, 10, 20, 20, tags="oval1", fill="blue")
    canvas.create_oval(50, 50, 80, 80, tags="oval2", fill="yellow")

    canvas.tag_bind("oval2", "<Enter>", turnRed)
    canvas.tag_bind("oval2", "<Leave>", turnYellow)

    canvas.bind("<Key-Return>", keypress)
    canvas.bind("<Key-KP_Enter>", keypress)

    canvas.focus_set()
    root.mainloop()

    Try pressing Return with the mouse pointer over oval2 and elsewhere on the
    canvas.

    Peter
    Peter Otten, Dec 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moondaddy
    Replies:
    3
    Views:
    38,213
    moondaddy
    Apr 28, 2004
  2. Matt Adams
    Replies:
    0
    Views:
    875
    Matt Adams
    Aug 26, 2003
  3. Kamaljeet Saini
    Replies:
    0
    Views:
    387
    Kamaljeet Saini
    Feb 13, 2009
  4. Replies:
    7
    Views:
    365
    Mick White
    Feb 2, 2005
  5. apparker
    Replies:
    10
    Views:
    358
    apparker
    Apr 3, 2007
Loading...

Share This Page