473,785 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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+Pytho nWin.

##----------------------
from Tkinter import *

class MyApp:
def __init__(self,p arent):
self.myContaine r1 = Frame(parent)
self.myContaine r1.pack()
self.canv = Canvas(relief=S UNKEN)
self.canv.confi g(width = 300,height=300)
self.canv.pack( )
self.canv.creat e_rectangle(100 ,100,150,150,ta gs="rect")
self.canv.bind( '<Up>',self._on UpKey)
self.canv.bind( '<Return>', self._onReturnK ey)
def _onUpKey(self,e vent):
self.canv.move( tagOrId,xAmount =0,yAmount=10)
def _onReturnKey(se lf,event):
print 'Hello world'
root = Tk()
myapp = MyApp(root)
root.mainloop()

##----------------------

Best regards,
Davy
Nov 20 '07 #1
4 2884
On Nov 19, 8:13 pm, Davy <zhushe...@gmai l.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+Pytho nWin.

##----------------------
from Tkinter import *

class MyApp:
def __init__(self,p arent):
self.myContaine r1 = Frame(parent)
self.myContaine r1.pack()
self.canv = Canvas(relief=S UNKEN)
self.canv.confi g(width = 300,height=300)
self.canv.pack( )
self.canv.creat e_rectangle(100 ,100,150,150,ta gs="rect")
self.canv.bind( '<Up>',self._on UpKey)
self.canv.bind( '<Return>', self._onReturnK ey)
def _onUpKey(self,e vent):
self.canv.move( tagOrId,xAmount =0,yAmount=10)
def _onReturnKey(se lf,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...mayb e 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,p arent):
self.myContaine r1 = Frame(parent)
self.myContaine r1.pack()
self.canv = Canvas(relief=S UNKEN)
self.canv.confi g(width = 300,height=300)
self.canv.pack( )
self.canv.creat e_rectangle(100 ,100,150,150,ta gs="rect")
self.canv.bind( '<Up>',self._on UpKey)
self.canv.bind( '<Return>', self._onReturnK ey)
def _onUpKey(self,e vent):
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,p arent):
self.myContaine r1 = Frame(parent)
self.myContaine r1.pack()
self.canv = Canvas(relief=S UNKEN)
self.canv.confi g(width = 300,height=300)
self.canv.pack( )
self.canv.creat e_rectangle(100 ,100,150,150,ta gs="rect")
self.canv.bind( '<Up>',self._on UpKey)
self.canv.bind( '<Return>', self._onReturnK ey)
def _onUpKey(self,e vent):
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...@gmai l.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+Pytho nWin.

##----------------------
from Tkinter import *

class MyApp:
def __init__(self,p arent):
self.myContaine r1 = Frame(parent)
self.myContaine r1.pack()
self.canv = Canvas(relief=S UNKEN)
self.canv.confi g(width = 300,height=300)
self.canv.pack( )
self.canv.creat e_rectangle(100 ,100,150,150,ta gs="rect")
self.canv.bind( '<Up>',self._on UpKey)
self.canv.bind( '<Return>', self._onReturnK ey)
def _onUpKey(self,e vent):
self.canv.move( tagOrId,xAmount =0,yAmount=10)
def _onReturnKey(se lf,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
5986
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 filling the remainder of the window. Mind you, this is a contrived test application to help me understand Tkinter and Python, not an actual application yet. I've trivially subclassed Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases...
3
7036
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 import * class App:
2
3250
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. My button bar is implemented a Frame subclass, which takes the button dictionary as an argument and displays the buttons on the screen: class OptionsBar(Frame): def __init__(self, buttonDict, parent=None) Frame.__init__(self, parent)
7
11908
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 wxPython. Mitchell Timin -- "Many are stubborn in pursuit of the path they have chosen, few in
0
3587
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 accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
2
4087
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 open a python interpreter and run my subprocess command, all is well. But when I run the same command from inside a Tkinter app, I'm getting errors.
1
3600
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 I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
1
5124
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 class countdown(Tkinter.Frame):
0
1545
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
3918
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 this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.