Tkinter focus problem

Didier WILLAME didier.willame at ieee.org
Fri Jun 30 12:12:56 EDT 2000


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



# ----------
 1 import Tkinter
 2 import Canvas
 3
 4 class Graph( Tkinter.Frame ):
 5
 6   # ----------
 7   def __init__( self, master=None ):
 8     Tkinter.Frame.__init__( self, master )
 9     self.pack()
10     self.createCanvas()
11     return
12
13   # ----------
14   def createCanvas( self ):
15     self.editingArea = Tkinter.Canvas( self )
16 
17     # --- To create a node with the left button.
18     self.editingArea.bind( '<Button-1>', self.mkNode )
19
20     # --- To toggle the node color when passing with the mouse.
21     self.editingArea.tag_bind( 'node', '<Any-Enter>', self.entryNode
)
22     self.editingArea.tag_bind( 'node', '<Any-Leave>', self.leaveNode
)
23
24     # --- To active a nodes.
25     self.editingArea.tag_bind( 'node', '<KeyPress-1>', self.srcNode )
26 #    self.editingArea.tag_bind( 'node', '<Button-2>', self.srcNode )
27
28     self.editingArea.pack( { 'side' : 'top' } )
29
30     return
31
32   # ----------
33   def mkNode( self, event ):
34     "Create a new node at (x,y)"
35
36     x = event.x
37     y = event.y
38
39     new = self.editingArea.create_oval( x-5, y-5, x+5, y+5 )
40     self.editingArea.itemconfigure( new, { 'tag'     : 'node'  } )
41     self.editingArea.itemconfigure( new, { 'outline' : 'black' } )
42     self.editingArea.itemconfigure( new, { 'fill'    : 'red'   } )
43
44     return
45
46   # ----------
47   def entryNode( self, event ):
48     self.editingArea.itemconfigure( 'current', { 'fill' : 'blue' } )
49    return
50
51   # ----------
52   def leaveNode( self, event ):
53     self.editingArea.itemconfigure( 'current', { 'fill' : 'red' } )
54     return
55
56   # ----------
57   def srcNode( self, event ):
58     print 'Hello World!'
59     return
60
61 # ----------
62 graph = Graph()
63 try:
64   graph.mainloop()
65 except KeyboardInterrupt:
66   graph.quit()
67 # ----------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: didier.willame.vcf
Type: text/x-vcard
Size: 453 bytes
Desc: Card for Didier WILLAME
URL: <http://mail.python.org/pipermail/python-list/attachments/20000630/8470d485/attachment.vcf>


More information about the Python-list mailing list