Tkinter Problem?

Davy zhushenli at gmail.com
Wed Nov 21 01:30:15 EST 2007


Hi all,

I have solved the problem after read some code. Because Tk.Canvas do
not have a focus, it does not receive a key input. The solution is
bind key input to the highest level 'root'

root.bind('<Up>',self._onUpKey)

Davy

On Nov 20, 10:13 am, 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




More information about the Python-list mailing list