通过python tkinter 实现仪表盘,实时刷新数据,指针等
https://download.youkuaiyun.com/download/csdn_wql_/89712114
https://download.youkuaiyun.com/download/csdn_wql_/89712083
import tkinter as tk
import math
# 圆形仪表类
class Gauge(tk.Canvas):
def __init__(self, master, title, max_value, label, data_y_position=130):
super().__init__(master, width=200, height=200)
self.title = title
self.max_value = max_value
self.label = label
self.current_value = 0
self.data_y_position = data_y_position
# 绘制圆形仪表
self.draw_gauge()
self.draw_pointer()
self.draw_title()
self.draw_value_label()
self.pack(side=tk.LEFT, padx=10, pady=10)
def draw_gauge(self):
# 绘制圆形外框
self.create_oval(10, 30, 140, 160, width=2, outline='gray')
# 根据角度计算刻度间隔,避免数字重叠
num_ticks = 12 # 总共显示12个刻度
tick_angle_interval = 360 / num_ticks
tick_value_interval = self.max_value / num_ticks
# 绘制刻度
for i in range(num_ticks):
angle = math.radians(i * tick_angle_interval)
x_offset = 60 if i % (num_ticks // 4) == 0 else 55 # 每1/4处为整刻度,数字离圆心更远
x = 75 + x_offset * math.sin(angle)
y = 95 - x_offset * math.cos(angle)
if i % (num_ticks // 4) == 0: # 每1/4处显示一个数字
# if (0 <= angle <= math.pi / 2) or (3 * math.pi / 2 <= angle <= 2 * math.pi):
# x += 5 * math.sin(angle) # 调整数字位置,尽量放在圆外合适位置
# else:
# x -= 5 * math.sin(angle)
# if math.pi / 2 <= angle <= 3 * math.pi / 2:
# y += 5 * math.cos(angle)
# else:
# y -= 5 * math.cos(angle)
self.create_text(x, y, text=str(int(i * tick_value_interval)), font=("Helvetica", 10))
else:
# 创建虚线刻度线
dash = (2, 2) # 虚线样式,2个像素实线,2个像素空白
self.create_line(75 + 50 * math.sin(angle), 95 - 50 * math.cos(angle),
75 + 53 * math.sin(angle), 95 - 53 * math.cos(angle), width=1, fill='gray', dash=dash)
def draw_pointer(self):
angle = math.radians(self.current_value * (360 / self.max_value))
x1 = 75
y1 = 95
x2 = 75 + 50 * math.sin(angle)
y2 = 95 - 50 * math.cos(angle)
self.pointer = self.create_line(x1, y1, x2, y2, width=3, arrow=tk.LAST, fill='blue')
def draw_title(self):
self.create_text(75, 10, text=self.title, font=("Helvetica", 12, "bold"))
def draw_value_label(self):
self.value_label = self.create_text(75, self.data_y_position, text=self.label + ": " + str(self.current_value),
font=("Helvetica", 10))
def update_value(self, new_value):
self.current_value = new_value
angle = math.radians(self.current_value * (360 / self.max_value))
x1 = 75
y1 = 95
x2 = 75 + 50 * math.sin(angle)
y2 = 95 - 50 * math.cos(angle)
self.coords(self.pointer, x1, y1, x2, y2)
self.itemconfig(self.value_label, text=self.label + ": " + str(self.current_value))
# 主窗口
root = tk.Tk()
root.title("Submarine Monitoring")
# 创建角度仪表,数据显示位置下移到140
angle_gauge = Gauge(root, "Angle", 360, "Angle(deg)", data_y_position=170)
# 创建深度仪表,数据显示位置下移到140
depth_gauge = Gauge(root, "Depth", 100, "Depth(m)", data_y_position=170)
# 创建水压仪表,数据显示位置下移到140
water_pressure_gauge = Gauge(root, "Water Pressure", 1000, "Pressure(hPa)", data_y_position=170)
# 创建气压仪表,数据显示位置下移到140
air_pressure_gauge = Gauge(root, "Air Pressure", 500, "Air Pressure(hPa)", data_y_position=170)
# 模拟更新值的函数(这里只是简单示例,可替换为真实数据获取和更新逻辑)
def update_gauge_values():
import random
angle_gauge.update_value(random.randint(0, 360))
depth_gauge.update_value(random.randint(0, 100))
water_pressure_gauge.update_value(random.randint(0, 1000))
air_pressure_gauge.update_value(random.randint(0, 500))
root.after(1000, update_gauge_values)
root.after(1000, update_gauge_values)
root.mainloop()