Tkinter focus problem
John Grayson
johngrayson at home.com
Fri Jun 30 13:25:47 EDT 2000
In article <395CC8DE.AA663D4 at ieee.org>,
Didier WILLAME <didier.willame at ieee.org> wrote:
> This is a multi-part message in MIME format.
> --------------EAB9DEA81EB868EA42827E00
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
>
> Hello all,
>
> I've slight problems with focus in Tkinter, running under win NT.
>
> With the following script I'm able to create 'nodes' (left button) in
a
> canvas. The color of a 'node' toggles if the mouse is putting on.
> Moreover, I would like display "Hello World!" when I press on the key
> '1' and, when the mouse is putting on this 'node'.
>
> But I'm unable to bind a key event (line 25), although I can run the
> script with a button event (line 26 instead of line 25).
> I should probably give the focus to the 'node' object inside the
> entryNode function (line 57).
>
> Thank you for your time,
> Didier
>
Well, you need to give the canvas focus. However, I don't think that
your original code is adequate, since I'm sure you will want to know
*which* node is active.
I've restructured your code a bit, storing the current item that
has been entered, giving it a new highlight when '1' is pressed.
Should get you going again...
John Grayson (Python and Tkinter Programming)
# ----------
from Tkinter import *
class Graph( Frame ):
# ----------
def __init__( self, master=None ):
Frame.__init__( self, master )
self.pack()
self.createCanvas()
self.activeNode = None
# ----------
def createCanvas( self ):
self.editingArea = Canvas( self )
# --- To create a node with the left button.
self.editingArea.bind( '<Button-1>', self.mkNode )
self.editingArea.bind( '<KeyPress-1>', self.activateNode )
self.editingArea.bind( '<Button-2>', self.activateNode )
# --- To toggle the node color when passing with the mouse.
self.editingArea.tag_bind( 'node', '<Any-Enter>', self.entryNode )
self.editingArea.tag_bind( 'node', '<Any-Leave>', self.leaveNode )
self.editingArea.pack(side=TOP)
self.editingArea.focus_set()
# ----------
def mkNode( self, event ):
"""Create a new node at (x,y)"""
x = event.x
y = event.y
new = self.editingArea.create_oval( x-5, y-5, x+5, y+5 )
self.editingArea.itemconfigure( new, tag='node', outline='black',
fill='red' )
# ----------
def entryNode( self, event ):
self.editingArea.itemconfigure( CURRENT, fill='blue' )
self.activeNode = self.editingArea.find_withtag( CURRENT )
# ----------
def leaveNode( self, event ):
self.editingArea.itemconfigure( CURRENT, fill= 'red' )
self.activeNode = None
# ----------
def srcNode( self, event=None ):
print 'Hello World!'
def activateNode( self, event ):
if self.activeNode:
self.srcNode()
self.editingArea.itemconfigure( self.activeNode, fill='green')
# -----------------------------------------------
graph = Graph()
try:
graph.mainloop()
except KeyboardInterrupt:
graph.quit()
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list