2022年 11月 7日

python怎么重复画圆_重画圆Python

我这里有一段代码:from tkinter import *

class player():

def __init__(self, radius, xcoordinate = 0, ycoordinate = 0):

self.xcoordinate = xcoordinate

self.ycoordinate = ycoordinate

self.radius = radius

def moveRight(self, event):

self.xcoordinate += 25

self.draw()

print(“Right key pressed”)

print(“x: ” + str(self.xcoordinate))

def moveLeft(self, event):

self.ycoordinate += 25

self.draw()

print(“Left key pressed”)

print(“y: ” + str(self.ycoordinate))

def draw(self):

world = client()

world.title(“World”)

world.bind(”, self.moveRight)

world.bind(”, self.moveLeft)

canvas = Canvas(world, width=200, height=200, borderwidth=0,highlightthickness=0, bg=”black”)

canvas.grid()

canvas.draw_player(self.xcoordinate, self.ycoordinate, self.radius, fill=”blue”, width=4)

world.mainloop()

class client(Tk):

def __init__(self):

super().__init__()

def draw_player(self, x, y, r, **kwargs):

return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)

Canvas.draw_player = draw_player

p1 = player(50)

p1.draw()

问题是每当我按下向右或向左箭头键时,它都会调用draw()方法。draw()方法构造一个新的客户端对象等

最后打开多个窗口,每个窗口都有一个x和y坐标不同的圆。如何使它在调用draw()时只编辑x和y坐标,并在同一窗口中重绘圆?在

请不要建议使用pygame,我的IDE在尝试导入模块时出错。在