from tkinter import *
x, y, a, b = 50, 50, 100, 100
d = None
vel_dic = {
"Left": ("left", -4, 0),
"Right": ("right", 4, 0),
"Down": ("down", 0, 4),
"Up": ("up", 0, -4)}
class Sprite:
def __init__(self):
self.move
self.x, self.y, self.a, self.b = 50, 50, 100, 100
self.d = 0
self.canvas = Canvas(tk, height = 600, width = 600)
self.canvas.grid(row=0, column=0, sticky = W)
self.coord = [self.x, self.y, self.a, self.b]
self.shape = self.canvas.create_rectangle(*self.coord, outline = "#cc9900", fill = "#cc9900")
def move():
if self.direction != 0:
self.canvas.move(self.rect, self.xv, self.yv)
tk.after(33, move)
def on_keypress(event):
self.direction, self.xv, self.yv = vel_dic[event.keysym]
def on_keyrelease(event):
self.direction = 0
tk = Tk()
tk.geometry("600x600")
sprite1 = Sprite()
tk.bind_all('<KeyPress>', sprite1.on_keypress)
tk.bind_all('<KeyRelease>', sprite1.on_keyrelease)
按右箭头键时的错误消息:
^{pr2}$
在对象内部调用函数时,
self
(对象的实例)作为第一个参数发送。您可以使用一些方法来撤消它,staticmethod
是最常见的方法,但在这种情况下,这不是您要寻找的。在您得到的错误表明解释器发送了这个
self
参数和常规的event
参数,但是您的方法只得到一个参数,并且无法处理它们。在确保除了其他参数之外,所有函数都将
self
(或您选择的任何名称,如inst
)作为第一个参数:move
和on_keypress
也是如此。在相关问题 更多 >
编程相关推荐