簡體   English   中英

Python Tkinter平滑移動

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM