473,378 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 2851
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
2
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make....
7
by: SeeBelow | last post by:
Do many people think that wxPython should replace Tkinter? Is this likely to happen? I ask because I have just started learning Tkinter, and I wonder if I should abandon it in favor of...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
1
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter ...
0
by: wolfonenet | last post by:
Hi All, My setup is: WinXP Python 2.5.1 TKinter version: $Revision: 50704 $ Tcl: 8.4 Debugger: WinPdb
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.