471,354 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Tkinter Problem?

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="r ect")
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
Nov 20 '07 #1
4 2701
On Nov 19, 8:13 pm, Davy <zhushe...@gmail.comwrote:
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="r ect")
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-...kinter-why.htm
http://www.pythonware.com/library/tk...d-bindings.htm
http://docs.huihoo.com/tkinter/an-in...97/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/tk...ion/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
Nov 20 '07 #2
On Mon, 19 Nov 2007 18:13:03 -0800, Davy wrote:
##----------------------
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="r ect")
self.canv.bind('<Up>',self._onUpKey)
self.canv.bind('<Return>', self._onReturnKey)
def _onUpKey(self,event):
self.canv.move(tagOrId,xAmount=0,yAmount=10)
Where's `tagOrId` coming from? That's a `NameError` here.

Ciao,
Marc 'BlackJack' Rintsch
Nov 20 '07 #3
Marc 'BlackJack' Rintsch wrote:
On Mon, 19 Nov 2007 18:13:03 -0800, Davy wrote:
>##----------------------
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="r ect")
self.canv.bind('<Up>',self._onUpKey)
self.canv.bind('<Return>', self._onReturnKey)
def _onUpKey(self,event):
self.canv.move(tagOrId,xAmount=0,yAmount=10)

Where's `tagOrId` coming from? That's a `NameError` here.
Also, the arguments of Canvas.move() are positional.

self.canv.move("rect", 0, 10)

should work though the direction of the move might surprise you.

Peter
Nov 20 '07 #4
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...@gmail.comwrote:
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="r ect")
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
Nov 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by srijit | last post: by
2 posts views Thread by Paul A. Wilson | last post: by
7 posts views Thread by SeeBelow | last post: by
reply views Thread by syed_saqib_ali | last post: by
2 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Michael Yanowitz | last post: by
1 post views Thread by vigacmoe | last post: by
reply views Thread by wolfonenet | last post: by
3 posts views Thread by J-Burns | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.