反转 Tkinter 上的 y 坐标 canvas 和基于多个时间的多个 canvas 对象的动画
Reversing y coordinates on Tkinter canvas and animation of multiple canvas object based on multiple time
我对 python、
上的库 Tkinter 有点陌生
我正在尝试为道路上车辆的实际时间和模拟时间制作动画。道路的路段是使用 GPS data.For 示例构建的,下面是我的数据框:
----------------------------------------------------
Easting | Northing | ActualTime | SimulatedTime
----------------------------------------------------
311257 | 6408368 | 5 | 4.5
311300 | 6408400 | 5 | 4
311305 | 6408410 | 5 | 4.2
......
我有两个问题:
1. 如何在 tkinter canvas 上反转 y 坐标?
2. 如何让不同时间移动的物体动画化?
我目前正在做的事情如下:
from tkinter import *
import pandas as pd
from sklearn import preprocessing
data = pd.read_csv('data.csv')
east = data[['Easting']].values
north = data[['Northing']].values
min_max_scaler = preprocessing.MinMaxScaler()
east_scaled = min_max_scaler.fit_transform(east)
north_scaled = min_max_scaler.fit_transform(north)
data['N_Easting'] = east_scaled
data['N_Northing'] = north_scaled
data['shiftX'] = 0
data['shiftY'] = 0
for i in range(1, len(data)):
data['shiftX'].loc[i] = data['N_Easting'].loc[i] - data['N_Easting'].loc[i-1]
data['shiftY'].loc[i] = data['FNorthing'].loc[i] - data['FNorthing'].loc[i-1]
master = Tk()
canvas_width = 1100
canvas_height = 1100
w = Canvas(master, width = canvas_width, height = canvas_height)
w.pack()
# creating the road segment
for i in range (0, len(data)-1):
b = w.create_line(data['N_Easting'].loc[i], data['N_Northing'].loc[i], data['N_Easting'].loc[i+1], data['N_Northing'].loc[i+1], width = 10)
a = w.create_oval(data['N_Easting'].loc[0]-5, data['N_Northing'].loc[0]-5, data['N_Easting'].loc[0]+5, data['N_Northing'].loc[0]+5, fill = 'red')
s = w.create_oval(data['N_Easting'].loc[0]-5, data['N_Northing'].loc[0]-5, data['N_Easting'].loc[0]+5, data['N_Northing'].loc[0]+5, fill = 'yellow')
for i in range(1, len(data)):
w.move(s, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['ActualTime'].loc[i])
for i in range(1, len(data)):
w.move(a, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['SimulatedTime'].loc[i])
mainloop()
但是,通过执行上面的脚本,实际物体正在移动并遍历路经,当它完成行程后,模拟物体开始遍历。任何人都可以帮助我让两个物体一起移动,我可以制作动画并比较实际和模拟车辆的行驶时间吗?
提前致谢!
不确定我是否理解正确,但您似乎希望两个 move
函数同时启动。如果是这样,您需要 thread
您的功能。
替换:
for i in range(1, len(data)):
w.move(s, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['ActualTime'].loc[i])
for i in range(1, len(data)):
w.move(a, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['SimulatedTime'].loc[i])
与:
import threading
def move_func(column):
for i in range(1, len(data)):
w.move(s, data['shiftX'].loc[i], data['shiftY'].loc[i])
time.sleep(data[column].loc[i])
threading.Thread(target=move_func, args=('ActualTime',)).start()
threading.Thread(target=move_func, args=('SimulatedTime',)).start()
我对 python、
上的库 Tkinter 有点陌生我正在尝试为道路上车辆的实际时间和模拟时间制作动画。道路的路段是使用 GPS data.For 示例构建的,下面是我的数据框:
----------------------------------------------------
Easting | Northing | ActualTime | SimulatedTime
----------------------------------------------------
311257 | 6408368 | 5 | 4.5
311300 | 6408400 | 5 | 4
311305 | 6408410 | 5 | 4.2
......
我有两个问题: 1. 如何在 tkinter canvas 上反转 y 坐标? 2. 如何让不同时间移动的物体动画化?
我目前正在做的事情如下:
from tkinter import *
import pandas as pd
from sklearn import preprocessing
data = pd.read_csv('data.csv')
east = data[['Easting']].values
north = data[['Northing']].values
min_max_scaler = preprocessing.MinMaxScaler()
east_scaled = min_max_scaler.fit_transform(east)
north_scaled = min_max_scaler.fit_transform(north)
data['N_Easting'] = east_scaled
data['N_Northing'] = north_scaled
data['shiftX'] = 0
data['shiftY'] = 0
for i in range(1, len(data)):
data['shiftX'].loc[i] = data['N_Easting'].loc[i] - data['N_Easting'].loc[i-1]
data['shiftY'].loc[i] = data['FNorthing'].loc[i] - data['FNorthing'].loc[i-1]
master = Tk()
canvas_width = 1100
canvas_height = 1100
w = Canvas(master, width = canvas_width, height = canvas_height)
w.pack()
# creating the road segment
for i in range (0, len(data)-1):
b = w.create_line(data['N_Easting'].loc[i], data['N_Northing'].loc[i], data['N_Easting'].loc[i+1], data['N_Northing'].loc[i+1], width = 10)
a = w.create_oval(data['N_Easting'].loc[0]-5, data['N_Northing'].loc[0]-5, data['N_Easting'].loc[0]+5, data['N_Northing'].loc[0]+5, fill = 'red')
s = w.create_oval(data['N_Easting'].loc[0]-5, data['N_Northing'].loc[0]-5, data['N_Easting'].loc[0]+5, data['N_Northing'].loc[0]+5, fill = 'yellow')
for i in range(1, len(data)):
w.move(s, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['ActualTime'].loc[i])
for i in range(1, len(data)):
w.move(a, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['SimulatedTime'].loc[i])
mainloop()
但是,通过执行上面的脚本,实际物体正在移动并遍历路经,当它完成行程后,模拟物体开始遍历。任何人都可以帮助我让两个物体一起移动,我可以制作动画并比较实际和模拟车辆的行驶时间吗?
提前致谢!
不确定我是否理解正确,但您似乎希望两个 move
函数同时启动。如果是这样,您需要 thread
您的功能。
替换:
for i in range(1, len(data)):
w.move(s, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['ActualTime'].loc[i])
for i in range(1, len(data)):
w.move(a, data['shiftX'].loc[i], data['shiftY'].loc[i])
master.update()
time.sleep(data['SimulatedTime'].loc[i])
与:
import threading
def move_func(column):
for i in range(1, len(data)):
w.move(s, data['shiftX'].loc[i], data['shiftY'].loc[i])
time.sleep(data[column].loc[i])
threading.Thread(target=move_func, args=('ActualTime',)).start()
threading.Thread(target=move_func, args=('SimulatedTime',)).start()