我正在尝试将画布圈移动到tkinter窗口。我使用canvas.move,但它只是让对象重新出现在新位置。我想真的看到它旅行。有没有办法做到这一点?我有以下内容:
def move_to(self, user_id, old_location, new_location):
self.user_list[user_id].set_location(new_location)
user_canvas_id = self.user_id_dict[user_id]
row_delta = new_location[ROW_INDEX] - old_location[ROW_INDEX]
col_delta = new_location[COL_INDEX] - old_location[COL_INDEX]
self.canvas.move(user_canvas_id, row_delta, col_delta)
def roaming_handler(self, user_id):
row = randrange(1, self.number_of_events * 125)
col = randrange(1, self.number_of_events * 125)
user_location = self.user_list[user_id].get_location()
self.move_to(user_id, user_location, (row, col))
当时模拟中还有其他各种各样的事情发生,其中很多都会在某个时刻称之为。
答案 0 :(得分:1)
我认为这就是你要找的东西:
您必须单击圆圈并将其拖动到新位置
import tkinter
# Create some constants
RADIUS = 50
START_POS = 10
TAG = 'circle'
# The dragging function
def drag_circle(event, canvas):
r = RADIUS / 2
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
canvas.coords(TAG, x - r, y - r, x + r, y + r)
# Create window and canvas
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack(fill=tkinter.BOTH, expand=True)
# Draw a circle
canvas.create_oval(
START_POS, START_POS, START_POS + RADIUS, START_POS + RADIUS,
fill='#555', width=0, tags=TAG)
# Bind function to event
canvas.tag_bind(TAG, '<B1-Motion>', lambda e: drag_circle(e, canvas))
# Run mainloop
root.mainloop()
答案 1 :(得分:0)
我认为您只需要在流程中添加延迟,这样您就可以“按照查看旅行”。你就是这样做的:
canvas.update()
canvas.after(#number of miliseconds)
示例代码:
import tkinter
canvas = tkinter.Canvas(width=600, height=200)
canvas.pack()
canvas.create_oval(250, 50, 350, 100)
for x in range(100):
canvas.move(1, -5, 0)
canvas.update()
canvas.after(100)