Python 特金特赢得';我不认识按键
我正试图编写一个程序,这样一辆汽车就可以在屏幕上移动,上下箭头键可以改变汽车的速度。但是,当我按下任一键时,都不会调用任何命令。我已经尝试将绑定线和焦点线更改为self.canvas,但也没有成功。我怎样才能解决这个问题Python 特金特赢得';我不认识按键,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我正试图编写一个程序,这样一辆汽车就可以在屏幕上移动,上下箭头键可以改变汽车的速度。但是,当我按下任一键时,都不会调用任何命令。我已经尝试将绑定线和焦点线更改为self.canvas,但也没有成功。我怎样才能解决这个问题 class racingCar: def __init__(self): window = Tk() window.title("Racing Car") self.cWidth = 500 self.c
class racingCar:
def __init__(self):
window = Tk()
window.title("Racing Car")
self.cWidth = 500
self.cHeight = 200
self.canvas = Canvas(window, width = self.cWidth, height = self.cHeight, bg = "white")
self.canvas.pack()
self.x = 0
self.y = self.cHeight
self.drawCar(self.x, self.y)
self.dx = 5
self.sleepTime = 50
self.animate()
window.bind_all("<Up>", self.increaseSpeed)
window.bind_all("<Down>", self.decreaseSpeed)
window.focus()
window.mainloop()
def drawCar(self, x, y):
self.canvas.create_rectangle(x, y - 20, x + 50, y - 10, fill = "yellow", tags = "car")
self.canvas.create_oval(x + 10, y - 10, x + 20, y, fill = "black", tags = "car")
self.canvas.create_oval(x + 30, y - 10, x + 40, y, fill = "black", tags = "car")
self.canvas.create_polygon([x + 10, y - 20, x + 20, y - 30, x + 30, y - 30, x + 40, y - 20], fill = "blue", tags = "car")
def increaseSpeed(self, event):
print("pressed up")
self.dx += 2.5
def decreaseSpeed(self, event):
print("pressed down")
if self.dx > 0:
self.dx -= 2.5
def animate(self):
while not self.dx == 0:
self.canvas.move("car", self.dx, 0)
self.canvas.after(self.sleepTime)
self.canvas.update()
if self.x < self.cWidth + 10:
self.x += self.dx
else:
self.x = -60
self.canvas.delete("car")
self.drawCar(self.x, self.y)
racingCar()
class赛车:
定义初始化(自):
window=Tk()
窗口标题(“赛车”)
self.cWidth=500
self.cHeight=200
self.canvas=canvas(窗口,宽度=self.cWidth,高度=self.cHeight,bg=“白色”)
self.canvas.pack()
self.x=0
self.y=self.cHeight
self.drawCar(self.x,self.y)
self.dx=5
self.sleepTime=50
self.animate()
window.bind\u all(“,self.increaseSpeed)
window.bind_all(“,self.decreaseSpeed)
window.focus()
window.mainloop()
def牵引车(自、x、y):
画布。创建矩形(x,y-20,x+50,y-10,fill=“yellow”,tags=“car”)
self.canvas.create_oval(x+10,y-10,x+20,y,fill=“black”,tags=“car”)
self.canvas.create_oval(x+30,y-10,x+40,y,fill=“black”,tags=“car”)
self.canvas.create_polygon([x+10,y-20,x+20,y-30,x+30,y-30,x+40,y-20],fill=“blue”,tags=“car”)
def递增速度(自身、事件):
打印(“向上压”)
self.dx+=2.5
def减速(自身、事件):
打印(“按下”)
如果self.dx>0:
self.dx-=2.5
def动画(自):
而不是self.dx==0:
self.canvas.move(“car”,self.dx,0)
self.canvas.after(self.sleepTime)
self.canvas.update()
如果self.x
当tkinter运行在同一个线程中时,您不应使用,
或sleep
在本例中,您如何使用after()
它的功能与sleep
完全相同
因为tkinter是单线程的,所以这些方法将阻塞主循环,并且在循环结束或睡眠结束之前不会发生任何其他事情。画布被更新的原因是,您正在while循环中调用update,但您的事件仍被阻止
要解决此问题,请使用after()
,因为它适用于tkinter中的这种循环
我已将您的代码更新为使用after()
,并对其进行了一些清理,以便更密切地遵循PEP8样式指南。如果你有任何问题,请告诉我
import tkinter as tk
class RacingCar(tk.Tk):
def __init__(self):
super().__init__()
self.title("Racing Car")
self.c_width = 500
self.c_height = 200
self.canvas = tk.Canvas(self, width=self.c_width, height=self.c_height, bg="white")
self.canvas.pack()
self.x = 0
self.y = self.c_height
self.draw_car(self.x, self.y)
self.dx = 5
self.after_time = 50
self.animate()
self.bind("<Up>", self.increase_speed)
self.bind("<Down>", self.decrease_speed)
def draw_car(self, x, y):
self.canvas.create_rectangle(x, y - 20, x + 50, y - 10, fill="yellow", tags="car")
self.canvas.create_oval(x + 10, y - 10, x + 20, y, fill="black", tags="car")
self.canvas.create_oval(x + 30, y - 10, x + 40, y, fill="black", tags="car")
self.canvas.create_polygon([x + 10, y - 20, x + 20, y - 30, x + 30, y - 30, x + 40, y - 20],
fill="blue", tags="car")
def increase_speed(self, _):
print("pressed up")
self.dx += 2.5
def decrease_speed(self, _):
print("pressed down")
if self.dx > 0:
self.dx -= 2.5
def animate(self):
if self.dx != 0:
self.canvas.move("car", self.dx, 0)
if self.x < self.c_width + 10:
self.x += self.dx
else:
self.x = -60
self.canvas.delete("car")
self.draw_car(self.x, self.y)
self.after(self.after_time, self.animate)
RacingCar().mainloop()
将tkinter作为tk导入
赛车等级(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
自我名称(“赛车”)
自身c_宽度=500
自身c_高度=200
self.canvas=tk.canvas(self,width=self.c_width,height=self.c_height,bg=“白色”)
self.canvas.pack()
self.x=0
self.y=self.c_高度
self.draw_车(self.x,self.y)
self.dx=5
赛后时间=50
self.animate()
self.bind(“,self.increase\u speed)
自绑定(“,自减速度)
def牵引车(自、x、y):
画布。创建矩形(x,y-20,x+50,y-10,fill=“yellow”,tags=“car”)
self.canvas.create_oval(x+10,y-10,x+20,y,fill=“black”,tags=“car”)
self.canvas.create_oval(x+30,y-10,x+40,y,fill=“black”,tags=“car”)
创建多边形([x+10,y-20,x+20,y-30,x+30,y-30,x+40,y-20],
fill=“blue”,tags=“car”)
def增加速度(自)
打印(“向上压”)
self.dx+=2.5
def降低速度(自身速度):
打印(“按下”)
如果self.dx>0:
self.dx-=2.5
def动画(自):
如果self.dx!=0:
self.canvas.move(“car”,self.dx,0)
如果self.x
尝试将函数racingCar()放入永久循环中?我假设您需要经常检查按键。mainloop()不是已经这样做了吗?或者我也必须调用eventloop()吗?我猜是while循环导致了这个问题。