I am trying to make a simple game in tkinter, and I need to bind the left/right arrow keys to a class method. The method also accepts an argument. I have done the binding, but when I click the arrow keys, nothing moves. I tried adding a print statement to the method, and when I click the keys, nothing prints, so the method isn't getting called. I don't think the binding is working, but I'm not sure how to fix it!
Also, is there a way to keep running a bound method if the user holds the bound key? Like click and holding the left key and having the method keep running over and over until the user lets go.
Thanks.
from Tkinter import *
from math import *
global radius
radius=175
root=Tk()
c=Canvas(root,width=600,height=600)
c.pack()
c.configure(scrollregion=(-300, -300, 300, 300))
c.create_oval((-40,-40,40,40),fill="Yellow",outline="Yellow")
def findcoords(angle,radius):
#cos,sin
a=cos(radians(angle))
b=sin(radians(angle))
return ((a*radius),(b*radius),(a*radius),(b*radius))
def createOutline(radius):
t=0
while t<=360:
c.create_rectangle(findcoords(t,radius))
t+=.01
class World():
def __init__(self,canvas,img=None,size=20,angle=90):
self.size=size
self.angle=angle
self.position=self.findcoord()
self.canvas=canvas
if img!=None:
self.img=img
self.current=c.create_image(self.position,image=self.img)
else:
self.current=c.create_oval(self.position,fill="Blue",outline="Blue")
c.bind('<Left>',lambda event,arg=.1:self.move(event,arg))
c.bind('<Right>',lambda event,arg=-.1:self.move(event,arg))
def findcoord(self):
#cos,sin
a=cos(radians(self.angle))
b=sin(radians(self.angle))
return (((a*radius)-self.size),((b*radius)-self.size),((a*radius)+self.size),((b*radius)+self.size))
def move(self,n):
self.angle+=.1
self.position=self.findcoord()
self.canvas.coords(self.current,self.position)
createOutline(175)
a=World(c)
root.mainloop()