import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
import math
import random
class CornerMarkModule:
def __init__(self, parent, width, height, module_id, label_text):
self.parent = parent
self.width = width
self.height = height
self.module_id = module_id
self.label_text = label_text
self.line_ids = []
self.animation_id = None
self.data_flow_points = [] # 存储数据流点
self.data_flow_animations = [] # 存储数据流动画ID
# 创建画布
self.canvas = tk.Canvas(
parent,
width=width,
height=height,
bg="#121212",
highlightthickness=0,
bd=0,
relief="flat"
)
self.canvas.pack(fill=tk.BOTH, expand=True)
# 计算中心位置
self.square_size = 80
self.start_x = width // 2 - self.square_size // 2
self.start_y = height // 2 - self.square_size // 2 - 10
# 创建蓝色圆角矩形
self.square = self.create_round_rect(
self.start_x, self.start_y,
self.start_x + self.square_size,
self.start_y + self.square_size,
radius=15,
fill="#325b74",
outline="#5a9bc0",
width=2
)
# 添加标签文本
self.label_id = self.canvas.create_text(
width // 2, height // 2 - 10,
text=label_text,
fill="#e0e0e0",
font=("Microsoft YaHei", 11, "bold"),
width=self.square_size - 10,
justify="center"
)
# 添加模块编号标签
self.canvas.create_text(
width // 2, height - 30,
text=f"模块 {module_id}",
fill="#7a7a7a",
font=("Microsoft YaHei", 9)
)
# 添加装饰线条
self.decor_line = self.canvas.create_line(
width//2 - 40, height - 25,
width//2 + 40, height - 25,
fill="#3a6b8c",
width=1,
dash=(4, 2)
)
# 初始参数
self.base_gap = 12
self.base_length = 30
self.create_all_lines()
# 动画计数器
self.counter = 0
# 绑定悬停效果
self.canvas.bind("<Enter>", self.on_enter)
self.canvas.bind("<Leave>", self.on_leave)
def on_enter(self, event):
"""鼠标悬停效果"""
self.canvas.itemconfig(self.square, fill="#5a9bc0")
for line_id in self.line_ids:
self.canvas.itemconfig(line_id, fill="#5a9bc0")
self.canvas.itemconfig(self.decor_line, fill="#5a9bc0")
def on_leave(self, event):
"""鼠标离开效果"""
self.canvas.itemconfig(self.square, fill="#325b74")
for line_id in self.line_ids:
self.canvas.itemconfig(line_id, fill="#3a6b8c")
self.canvas.itemconfig(self.decor_line, fill="#3a6b8c")
def create_round_rect(self, x1, y1, x2, y2, radius=25, **kwargs):
"""创建圆角矩形"""
points = []
# 左上角
points.append(x1 + radius)
points.append(y1)
points.append(x2 - radius)
points.append(y1)
points.append(x2)
points.append(y1)
points.append(x2)
points.append(y1 + radius)
# 右上角
points.append(x2)
points.append(y2 - radius)
points.append(x2)
points.append(y2)
points.append(x2 - radius)
points.append(y2)
# 右下角
points.append(x1 + radius)
points.append(y2)
points.append(x1)
points.append(y2)
points.append(x1)
points.append(y2 - radius)
# 左下角
points.append(x1)
points.append(y1 + radius)
points.append(x1)
points.append(y1)
points.append(x1 + radius)
points.append(y1)
return self.canvas.create_polygon(points, **kwargs, smooth=True)
def create_all_lines(self):
line_color = "#3a6b8c"
# 左上角标记
self.line_ids.append(self.canvas.create_line(
self.start_x - self.base_gap, self.start_y - self.base_gap,
self.start_x - self.base_gap + self.base_length, self.start_y - self.base_gap,
width=2, fill=line_color
))
self.line_ids.append(self.canvas.create_line(
self.start_x - self.base_gap, self.start_y - self.base_gap,
self.start_x - self.base_gap, self.start_y - self.base_gap + self.base_length,
width=2, fill=line_color
))
# 右上角标记
self.line_ids.append(self.canvas.create_line(
self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap,
self.start_x + self.square_size + self.base_gap - self.base_length, self.start_y - self.base_gap,
width=2, fill=line_color
))
self.line_ids.append(self.canvas.create_line(
self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap,
self.start_x + self.square_size + self.base_gap, self.start_y - self.base_gap + self.base_length,
width=2, fill=line_color
))
# 左下角标记
self.line_ids.append(self.canvas.create_line(
self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap,
self.start_x - self.base_gap + self.base_length, self.start_y + self.square_size + self.base_gap,
width=2, fill=line_color
))
self.line_ids.append(self.canvas.create_line(
self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap,
self.start_x - self.base_gap, self.start_y + self.square_size + self.base_gap - self.base_length,
width=2, fill=line_color
))
# 右下角标记
self.line_ids.append(self.canvas.create_line(
self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap,
self.start_x + self.square_size + self.base_gap - self.base_length, self.start_y + self.square_size + self.base_gap,
width=2, fill=line_color
))
self.line_ids.append(self.canvas.create_line(
self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap,
self.start_x + self.square_size + self.base_gap, self.start_y + self.square_size + self.base_gap - self.base_length,
width=2, fill=line_color
))
def update_animation(self):
# 使用正弦函数创建平滑的距离变化
phase = self.counter * 0.08
self.counter += 1
# 每个模块有不同的相位偏移
phase_offset = self.module_id * 0.5
distance_factor = 0.5 * math.sin(phase + phase_offset) + 0.5
# 计算当前间距和线长
current_gap = 5 + distance_factor * 20
current_length = 25 + distance_factor * 15
# 更新所有线条位置和长度
self.update_lines(current_gap, current_length)
# 继续动画循环并保存ID
self.animation_id = self.parent.after(50, self.update_animation)
def pause_animation(self):
"""暂停动画"""
if self.animation_id:
self.parent.after_cancel(self.animation_id)
self.animation_id = None
def resume_animation(self):
"""恢复动画"""
if not self.animation_id:
self.update_animation()
def reset_counter(self):
"""重置计数器"""
self.counter = 0
def update_lines(self, gap, length):
# 更新左上角水平线
self.canvas.coords(
self.line_ids[0],
self.start_x - gap, self.start_y - gap,
self.start_x - gap + length, self.start_y - gap
)
# 更新左上角垂直线
self.canvas.coords(
self.line_ids[1],
self.start_x - gap, self.start_y - gap,
self.start_x - gap, self.start_y - gap + length
)
# 更新右上角水平线
self.canvas.coords(
self.line_ids[2],
self.start_x + self.square_size + gap, self.start_y - gap,
self.start_x + self.square_size + gap - length, self.start_y - gap
)
# 更新右上角垂直线
self.canvas.coords(
self.line_ids[3],
self.start_x + self.square_size + gap, self.start_y - gap,
self.start_x + self.square_size + gap, self.start_y - gap + length
)
# 更新左下角水平线
self.canvas.coords(
self.line_ids[4],
self.start_x - gap, self.start_y + self.square_size + gap,
self.start_x - gap + length, self.start_y + self.square_size + gap
)
# 更新左下角垂直线
self.canvas.coords(
self.line_ids[5],
self.start_x - gap, self.start_y + self.square_size + gap,
self.start_x - gap, self.start_y + self.square_size + gap - length
)
# 更新右下角水平线
self.canvas.coords(
self.line_ids[6],
self.start_x + self.square_size + gap, self.start_y + self.square_size + gap,
self.start_x + self.square_size + gap - length, self.start_y + self.square_size + gap
)
# 更新右下角垂直线
self.canvas.coords(
self.line_ids[7],
self.start_x + self.square_size + gap, self.start_y + self.square_size + gap,
self.start_x + self.square_size + gap, self.start_y + self.square_size + gap - length
)
def get_center(self):
"""获取模块中心位置"""
return (self.width // 2, self.height // 2 - 10)
def create_data_flow_point(self, color="#5a9bc0"):
"""创建数据流点"""
point = self.canvas.create_oval(0, 0, 0, 0, fill=color, outline="")
self.data_flow_points.append(point)
return point
def animate_data_flow(self, points, point_id, speed=0.02):
"""动画数据流点沿路径移动"""
if not hasattr(self, 'path_index'):
self.path_index = 0
if self.path_index >= len(points):
self.path_index = 0
x, y = points[self.path_index]
r = 4
self.canvas.coords(point_id, x-r, y-r, x+r, y+r)
self.path_index += 1
# 随机变化速度
variation = random.uniform(0.8, 1.2)
delay = int(50 * variation)
animation_id = self.parent.after(delay, lambda: self.animate_data_flow(points, point_id, speed))
self.data_flow_animations.append(animation_id)
return animation_id
class DataFlowVisualizer:
def __init__(self, parent, modules):
self.parent = parent
self.modules = modules
self.connections = []
self.flow_lines = []
self.flow_points = []
# 创建画布用于绘制连接线
self.canvas = tk.Canvas(
parent,
bg="#121212",
highlightthickness=0,
bd=0
)
self.canvas.place(relx=0, rely=0, relwidth=1, relheight=1)
self.canvas.lower() # 置于底层
# 定义模块连接关系
self.define_connections()
def define_connections(self):
"""定义模块之间的连接关系"""
# 干扰(1) -> 威胁分析(2)
self.add_connection(1, 2, "#ff6b6b")
# 威胁分析(2) -> 知识库(4)
self.add_connection(2, 4, "#4ecdc4")
# 威胁分析(2) -> 推理引擎(5)
self.add_connection(2, 5, "#ffd166")
# 推理引擎(5) -> 决策输出(6)
self.add_connection(5, 6, "#1a936f")
# 决策输出(6) -> 知识库(4)
self.add_connection(6, 4, "#9b5de5")
# 抗干扰中(3) -> 干扰(1) [特殊连接]
self.add_connection(3, 1, "#5a9bc0")
def add_connection(self, from_id, to_id, color):
"""添加连接关系"""
self.connections.append({
'from': from_id,
'to': to_id,
'color': color
})
def draw_connections(self):
"""绘制所有连接线"""
for conn in self.connections:
from_module = self.get_module_by_id(conn['from'])
to_module = self.get_module_by_id(conn['to'])
if from_module and to_module:
# 获取模块在父容器中的位置
from_x = from_module.winfo_x() + from_module.get_center()[0]
from_y = from_module.winfo_y() + from_module.get_center()[1]
to_x = to_module.winfo_x() + to_module.get_center()[0]
to_y = to_module.winfo_y() + to_module.get_center()[1]
# 绘制连接线
line_id = self.canvas.create_line(
from_x, from_y, to_x, to_y,
fill=conn['color'],
width=2,
dash=(6, 4),
arrow=tk.LAST,
arrowshape=(12, 15, 5)
)
self.flow_lines.append(line_id)
# 创建流动数据点
self.create_flow_point(from_x, from_y, to_x, to_y, conn['color'])
def get_module_by_id(self, module_id):
"""根据ID获取模块实例"""
for module in self.modules:
if module.module_id == module_id:
return module
return None
def create_flow_point(self, from_x, from_y, to_x, to_y, color):
"""创建流动的数据点"""
# 生成路径点
points = self.generate_path_points(from_x, from_y, to_x, to_y)
# 创建点
point = self.canvas.create_oval(0, 0, 0, 0, fill=color, outline="")
self.flow_points.append(point)
# 启动动画
self.animate_point(point, points)
def generate_path_points(self, from_x, from_y, to_x, to_y, num_points=30):
"""生成路径点(直线或曲线)"""
points = []
# 直线路径
for i in range(num_points):
t = i / (num_points - 1)
x = from_x + (to_x - from_x) * t
y = from_y + (to_y - from_y) * t
points.append((x, y))
return points
def animate_point(self, point, points, index=0):
"""动画点沿路径移动"""
if index >= len(points):
index = 0
x, y = points[index]
r = 5
self.canvas.coords(point, x-r, y-r, x+r, y+r)
# 随机速度变化
speed = random.randint(1, 3)
self.parent.after(50, lambda: self.animate_point(point, points, index + speed))
def create_corner_marks():
# 创建主窗口
root = ttk.Window(title="动态模块系统", themename="superhero")
root.configure(bg="#121212")
# 设置窗口大小并居中
window_width = 1400
window_height = 800
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
root.geometry(f"{window_width}x{window_height}+{center_x}+{center_y}")
# 创建主容器框架
main_container = ttk.Frame(root, padding=(30, 20, 30, 10))
main_container.pack(fill=tk.BOTH, expand=True)
# 创建标题
header_frame = ttk.Frame(main_container)
header_frame.pack(fill=tk.X, pady=(0, 20))
title_label = ttk.Label(
header_frame,
text="动态模块系统",
font=("Microsoft YaHei", 18, "bold"),
bootstyle="inverse-primary"
)
title_label.pack(side=tk.LEFT)
# 添加副标题
subtitle = ttk.Label(
header_frame,
text="实时监控与响应系统",
font=("Microsoft YaHei", 12),
bootstyle="secondary"
)
subtitle.pack(side=tk.LEFT, padx=(15, 0), pady=(5, 0))
# 添加控制按钮
control_frame = ttk.Frame(header_frame)
control_frame.pack(side=tk.RIGHT)
# 创建网格容器
grid_container = ttk.Frame(main_container)
grid_container.pack(fill=tk.BOTH, expand=True)
# 配置网格行和列权重
grid_container.grid_rowconfigure(0, weight=1) # 第1行
grid_container.grid_rowconfigure(1, weight=1) # 第2行
grid_container.grid_rowconfigure(2, weight=1) # 第3行
grid_container.grid_columnconfigure(0, weight=1) # 第1列
grid_container.grid_columnconfigure(1, weight=1) # 第2列
grid_container.grid_columnconfigure(2, weight=1) # 第3列
grid_container.grid_columnconfigure(3, weight=1) # 第4列
# 定义6个模块的标签文本
labels = ["干扰", "威胁分析", "抗干扰中", "知识库", "推理引擎", "决策输出"]
# 创建模块容器
modules = []
module_frames = [] # 存储模块框架
# 抗干扰中 - 第1行第2列 (行0, 列1)
cell0 = ttk.Frame(grid_container)
cell0.grid(row=0, column=1, padx=15, pady=15, sticky="nsew")
module0 = CornerMarkModule(
cell0,
width=220,
height=280,
module_id=3,
label_text=labels[2]
)
modules.append(module0)
module_frames.append(cell0)
# 知识库 - 第3行第2列 (行2, 列1)
cell1 = ttk.Frame(grid_container)
cell1.grid(row=2, column=1, padx=15, pady=15, sticky="nsew")
module1 = CornerMarkModule(
cell1,
width=220,
height=280,
module_id=4,
label_text=labels[3]
)
modules.append(module1)
module_frames.append(cell1)
# 干扰 - 第2行第1列 (行1, 列0)
cell2 = ttk.Frame(grid_container)
cell2.grid(row=1, column=0, padx=15, pady=15, sticky="nsew")
module2 = CornerMarkModule(
cell2,
width=220,
height=280,
module_id=1,
label_text=labels[0]
)
modules.append(module2)
module_frames.append(cell2)
# 威胁分析 - 第2行第2列 (行1, 列1)
cell3 = ttk.Frame(grid_container)
cell3.grid(row=1, column=1, padx=15, pady=15, sticky="nsew")
module3 = CornerMarkModule(
cell3,
width=220,
height=280,
module_id=2,
label_text=labels[1]
)
modules.append(module3)
module_frames.append(cell3)
# 推理引擎 - 第2行第3列 (行1, 列2)
cell4 = ttk.Frame(grid_container)
cell4.grid(row=1, column=2, padx=15, pady=15, sticky="nsew")
module4 = CornerMarkModule(
cell4,
width=220,
height=280,
module_id=5,
label_text=labels[4]
)
modules.append(module4)
module_frames.append(cell4)
# 决策输出 - 第2行第4列 (行1, 列3)
cell5 = ttk.Frame(grid_container)
cell5.grid(row=1, column=3, padx=15, pady=15, sticky="nsew")
module5 = CornerMarkModule(
cell5,
width=220,
height=280,
module_id=6,
label_text=labels[5]
)
modules.append(module5)
module_frames.append(cell5)
# 启动所有模块的动画
for module in modules:
module.update_animation()
# 更新UI以确保位置信息正确
root.update()
# 创建数据流可视化
flow_visualizer = DataFlowVisualizer(grid_container, modules)
flow_visualizer.draw_connections()
# 添加系统状态标签
status_frame = ttk.Frame(main_container)
status_frame.pack(fill=tk.X, pady=(15, 5))
status_label = ttk.Label(
status_frame,
text="系统状态: 运行中 | 数据流: 正常 | 威胁级别: 低",
font=("Microsoft YaHei", 10),
bootstyle="success"
)
status_label.pack(side=tk.LEFT)
root.mainloop()
if __name__ == "__main__":
create_corner_marks()修改这段代码的错误,不要增加别的功能,修改好我提供的代码,写出修改后的完整代码