[Tutor] Keypress event in canvas..........

Python-lover hemanexp@yahoo.com
Sat May 24 13:03:43 2003


--0-1924752861-1053795694=:29590
Content-Type: text/plain; charset=us-ascii

Hi,  iam using python and Tkinter. Is it possible to bind a keyboard event, say "Up" arrow press, to a canvas item (like rectangle)? i have created a canvas and drawn a set of rectangles one above another.Now when i press "Up" arrow key first time i expect first rectangle to get focus and second one for  next keypress and so on utill you reach the last rectangle.  how can i do it?   My sample code is given below:***************************************from Tkinter import *
class App:
  def __init__(self,parent):
  self.myparent = parent
  self.canvas = Canvas(self.myparent,width=600,height=500)
  self.canvas.pack()
  x1 = 100
  y1 = 100
  x2 = 200
  y2 = 200
  i = 1
  while i<4:
          self.canvas.create_rectangle(x1,y1,x2,y2,tag='RECT',fill='red')
          y1 = y2
          y2 = y1 + 100
          i = i+1  self.canvas.tag_bind('RECT','<Up>', self.get_rect_points)  def get_rect_points():
   #.......................
  # Here i want to get rectangle points
  #........................        
root = Tk()
myapp =  App(root)
root.mainloop()  Thanx. 

---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
--0-1924752861-1053795694=:29590
Content-Type: text/html; charset=us-ascii

<DIV>Hi,</DIV>
<DIV>&nbsp; iam using python and Tkinter. Is it possible to bind a keyboard event, say&nbsp;"Up" arrow press, to a canvas item (like rectangle)? i have created a canvas and drawn a set of rectangles one above another.Now when i press&nbsp;"Up" arrow key first time i expect&nbsp;first rectangle to get focus and second one for&nbsp; next keypress and so on utill you reach the last rectangle.  how can i do it?</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; My sample code is given below:</DIV>
<DIV>***************************************</DIV>
<DIV>from Tkinter import *<BR>class App:<BR>&nbsp; def __init__(self,parent):<BR>&nbsp;&nbsp;self.myparent = parent<BR>&nbsp;&nbsp;self.canvas = Canvas(self.myparent,width=600,height=500)<BR>&nbsp;&nbsp;self.canvas.pack()<BR>&nbsp;&nbsp;x1 = 100<BR>&nbsp;&nbsp;y1 = 100<BR>&nbsp;&nbsp;x2 = 200<BR>&nbsp;&nbsp;y2 = 200<BR>&nbsp;&nbsp;i = 1<BR>&nbsp;&nbsp;while i&lt;4:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;self.canvas.create_rectangle(x1,y1,x2,y2,tag='RECT',fill='red')<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;y1 = y2<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;y2 = y1 + 100<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;i = i+1</DIV>
<DIV>&nbsp;&nbsp;self.canvas.tag_bind('RECT','&lt;Up&gt;', self.get_rect_points)</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;def get_rect_points():<BR>&nbsp; &nbsp;#.......................<BR>&nbsp;&nbsp;# Here i want to get rectangle points<BR>&nbsp;&nbsp;#........................</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<BR>root = Tk()<BR>myapp =&nbsp; App(root)<BR>root.mainloop()</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Thanx.</DIV>
<DIV>&nbsp;</DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*http://search.yahoo.com">The New Yahoo! Search</a> - Faster. Easier. Bingo.
--0-1924752861-1053795694=:29590--