Files
2026-06-16 15:40:19 +08:00

94 lines
3.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'''6. 简易 UI 模块midware/ui/main_ui.py
基于 tkinter 实现基础主界面,包含配置文件导入、外设配置表、数据传输状态、原始数据查看,满足用户界面需求:
'''
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
from loguru import logger
from midware.config.excel_config import load_csv_config
from midware.config.base_config import DEVICE_CONFIG_DICT, BUS_CONFIG
def load_config_file():
"""导入Excel配置文件"""
file_path = filedialog.askopenfilename(
title="选择外设配置文件",
filetypes=[("Excel文件", "*.xlsx;*.xls"), ("所有文件", "*.*")]
)
if not file_path:
return
try:
global DEVICE_CONFIG_DICT
DEVICE_CONFIG_DICT = load_csv_config(file_path)
# 刷新外设配置表
refresh_device_table()
messagebox.showinfo("成功", f"导入配置文件成功,共{len(DEVICE_CONFIG_DICT)}个外设")
logger.info(f"手动导入配置文件:{os.path.basename(file_path)}")
except Exception as e:
messagebox.showerror("失败", f"导入配置文件失败:{str(e)}")
logger.error(f"导入配置文件失败:{str(e)}", exc_info=True)
def refresh_device_table():
"""刷新外设配置表"""
# 清空原有数据
for item in device_tree.get_children():
device_tree.delete(item)
# 插入新数据
for dev_name, dev_info in DEVICE_CONFIG_DICT.items():
device_tree.insert(
"", tk.END,
values=(
dev_name,
dev_info.get("protocol", ""),
hex(dev_info.get("base_addr", 0)),
hex(dev_info.get("offset_addr", 0)),
dev_info.get("udp_ip", ""),
dev_info.get("udp_port", ""),
dev_info.get("desc", ""),
dev_info.get("send_cache", ""),
dev_info.get("recv_cache", "")
)
)
def show_raw_data():
"""查看原始数据(预留接口,后续实现时间排序展示)"""
messagebox.showinfo("提示", "原始数据查看功能开发中,将按时间排序展示所有单机数据")
def start_main_ui():
"""启动主界面"""
root = tk.Tk()
root.title("虚拟仿真平台中间层软件")
root.geometry("1200x600")
root.resizable(True, True)
# 顶部按钮栏
top_frame = ttk.Frame(root, padding="10")
top_frame.pack(fill=tk.X)
ttk.Button(top_frame, text="导入Excel配置", command=load_config_file).grid(row=0, column=0, padx=5)
ttk.Button(top_frame, text="查看原始数据", command=show_raw_data).grid(row=0, column=1, padx=5)
# 外设配置表
global device_tree
columns = ("名称", "协议", "基地址", "偏移地址", "UDPIP", "UDPPort", "说明", "发送缓存", "接收缓存")
device_tree = ttk.Treeview(root, columns=columns, show="headings", height=20)
# 设置列标题
for col in columns:
device_tree.heading(col, text=col)
device_tree.column(col, width=120, anchor=tk.CENTER)
device_tree.pack(fill=tk.BOTH, expand=True, padding="10")
# 数据传输状态栏
status_frame = ttk.Frame(root, padding="10")
status_frame.pack(fill=tk.X)
ttk.Label(status_frame, text="数据传输状态:").grid(row=0, column=0)
status_var = tk.StringVar(value="运行中已连接UDP/缓存)")
ttk.Label(status_frame, textvariable=status_var, foreground="green").grid(row=0, column=1)
# 初始化配置表
refresh_device_table()
# 主循环
root.mainloop()
if __name__ == "__main__":
start_main_ui()