0

我正在尝试在 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))

当时的模拟中还发生了许多其他事情,其中​​许多人会在某个时候或另一个时候称之为这个。

4

2 回答 2

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()
于 2013-09-12T12:44:56.077 回答
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)
于 2017-01-11T21:13:24.493 回答