|
| 1 | +import tkinter as tk |
| 2 | + |
| 3 | +SENSOR_SPACING = 60 # 每个传感器之间的垂直间隔 |
| 4 | +TICK_INTERVAL = 100 # 时间刻度线间隔(ms) |
| 5 | + |
| 6 | +# 示例传感器数据:{sensor_name: [timestamp1, timestamp2, ...]} |
| 7 | +sensor_data = { |
| 8 | + "IMU": [100, 300, 500, 800], |
| 9 | + "Cam": [150, 350, 700], |
| 10 | + "Lid": [200, 600, 900] |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +class TimelineApp: |
| 15 | + def __init__(self, root): |
| 16 | + self.root = root |
| 17 | + self.canvas = tk.Canvas(root, bg="white") |
| 18 | + self.canvas.pack(fill=tk.BOTH, expand=True) |
| 19 | + |
| 20 | + self.time_scale = 1.0 # px / ms,初始比例 |
| 21 | + self.time_offset = 0 # ms,时间轴左端对应时间(平移偏移) |
| 22 | + |
| 23 | + self.dragging = False |
| 24 | + self.last_x = 0 |
| 25 | + |
| 26 | + self.canvas.bind("<ButtonPress-1>", self.on_drag_start) |
| 27 | + self.canvas.bind("<B1-Motion>", self.on_drag_motion) |
| 28 | + self.canvas.bind("<MouseWheel>", self.on_mousewheel) # Windows/Mac |
| 29 | + self.canvas.bind("<Button-4>", self.on_mousewheel) # Linux 鼠标滚轮向上 |
| 30 | + self.canvas.bind("<Button-5>", self.on_mousewheel) # Linux 鼠标滚轮向下 |
| 31 | + |
| 32 | + # 绑定窗口尺寸变化事件 |
| 33 | + self.canvas.bind("<Configure>", lambda e: self.draw()) |
| 34 | + |
| 35 | + self.draw() |
| 36 | + |
| 37 | + def draw(self): |
| 38 | + self.canvas.delete("all") |
| 39 | + width = self.canvas.winfo_width() |
| 40 | + height = self.canvas.winfo_height() |
| 41 | + |
| 42 | + sensors = list(sensor_data.keys()) |
| 43 | + |
| 44 | + # 画每个传感器的时间轴 |
| 45 | + for idx, sensor in enumerate(sensors): |
| 46 | + y = 50 + idx * SENSOR_SPACING |
| 47 | + self.canvas.create_text(30, y, text=sensor, anchor="e") |
| 48 | + self.canvas.create_line(40, y, width - 20, y, fill="gray", dash=(2, 2)) |
| 49 | + |
| 50 | + for t in sensor_data[sensor]: |
| 51 | + # 时间根据偏移和缩放计算X坐标 |
| 52 | + x = 40 + (t - self.time_offset) * self.time_scale |
| 53 | + if 40 <= x <= width - 20: |
| 54 | + self.canvas.create_line(x, y - 10, x, y + 10, fill="blue") |
| 55 | + self.canvas.create_text(x, y + 15, text=str(t), anchor="n", font=("Arial", 8)) |
| 56 | + |
| 57 | + # 画时间刻度线 |
| 58 | + max_time_display = self.time_offset + (width - 60) / self.time_scale |
| 59 | + t = (self.time_offset // TICK_INTERVAL) * TICK_INTERVAL |
| 60 | + while t <= max_time_display: |
| 61 | + x = 40 + (t - self.time_offset) * self.time_scale |
| 62 | + self.canvas.create_line(x, 20, x, height - 20, fill="lightgray", dash=(1, 2)) |
| 63 | + self.canvas.create_text(x, 20, text=str(int(t)), anchor="s", font=("Arial", 8)) |
| 64 | + t += TICK_INTERVAL |
| 65 | + |
| 66 | + def on_drag_start(self, event): |
| 67 | + self.dragging = True |
| 68 | + self.last_x = event.x |
| 69 | + |
| 70 | + def on_drag_motion(self, event): |
| 71 | + if self.dragging: |
| 72 | + dx = event.x - self.last_x |
| 73 | + self.last_x = event.x |
| 74 | + self.time_offset -= dx / self.time_scale |
| 75 | + if self.time_offset < 0: |
| 76 | + self.time_offset = 0 |
| 77 | + self.draw() |
| 78 | + |
| 79 | + def on_mousewheel(self, event): |
| 80 | + if event.num == 4 or event.delta > 0: |
| 81 | + factor = 1.1 |
| 82 | + else: |
| 83 | + factor = 0.9 |
| 84 | + |
| 85 | + width = self.canvas.winfo_width() |
| 86 | + mouse_time = self.time_offset + (event.x - 40) / self.time_scale |
| 87 | + |
| 88 | + new_scale = self.time_scale * factor |
| 89 | + new_scale = max(0.1, min(new_scale, 10)) |
| 90 | + |
| 91 | + self.time_offset = mouse_time - (event.x - 40) / new_scale |
| 92 | + if self.time_offset < 0: |
| 93 | + self.time_offset = 0 |
| 94 | + |
| 95 | + self.time_scale = new_scale |
| 96 | + self.draw() |
0 commit comments