Tkinter Problem?

kyosohma at gmail.com kyosohma at gmail.com
Mon Nov 19 22:51:56 EST 2007


On Nov 19, 8:13 pm, Davy <zhushe... at gmail.com> wrote:
> Hi all,
>
> I have written a simple Tkinter program, that is draw a rectangle in a
> canvas, when I press Up key, the rectangle move up. But the program
> seems work not properly? My environment is Python2.5+PythonWin.
>
> ##----------------------
> from Tkinter import *
>
> class MyApp:
>     def __init__(self,parent):
>         self.myContainer1 = Frame(parent)
>         self.myContainer1.pack()
>         self.canv = Canvas(relief=SUNKEN)
>         self.canv.config(width = 300,height=300)
>         self.canv.pack()
>         self.canv.create_rectangle(100,100,150,150,tags="rect")
>         self.canv.bind('<Up>',self._onUpKey)
>         self.canv.bind('<Return>', self._onReturnKey)
>     def _onUpKey(self,event):
>         self.canv.move(tagOrId,xAmount=0,yAmount=10)
>     def _onReturnKey(self,event):
>         print 'Hello world'
>
> root = Tk()
> myapp = MyApp(root)
> root.mainloop()
>
> ##----------------------
>
> Best regards,
> Davy

I'm not sure, but I don't think you can bind the a key to a
canvas...you'd want to bind to the rectangle itself somehow.

Some articles on binding:

http://effbot.org/pyfaq/i-can-t-get-key-bindings-to-work-in-tkinter-why.htm
http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
http://docs.huihoo.com/tkinter/an-introduction-to-tkinter-1997/intro06.htm

Info on the Canvas object:

# this one talks about setting the focus on an item within the canvas
so that it has key bindings...maybe that will work.
http://effbot.org/tkinterbook/canvas.htm
http://www.pythonware.com/library/tkinter/introduction/canvas.htm

I am not a Tkinter expert. I am much better with wxPython, but I am
only starting to be able to answer questions on that topic.

Hopefully someone with more knowledge will show up shortly.

Mike



More information about the Python-list mailing list