[英]Python Tkinter Smooth Move
我正在尝试在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))
当时,模拟中还发生了许多其他事情,其中许多事情会在某一时刻或另一时刻发生。
我认为这是您要寻找的:
您必须单击圆圈并将其拖动到新位置
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()
我认为您只需要增加该过程的延迟 ,就可以按照您所说的“ 看到它运行 ”。 这是您的操作方式:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.