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 # 初始化动画ID属性
# 创建画布 - 不再使用外部容器框架
self.canvas = tk.Canvas(
parent,
width=width,
height=height,
bg="#121212", # 使用深色背景
highlightthickness=0,
bd=0,
relief="flat"
)
self.canvas.pack(side=tk.LEFT, padx=15, pady=15)
# 计算中心位置
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 create_corner_marks():
# 创建主窗口
root = ttk.Window(title="动态模块系统", themename="superhero")
root.configure(bg="#121212")
# 设置窗口大小并居中
window_width = 1400
window_height = 600
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)
# 定义6个模块的标签文本(中文)
labels = ["干扰", "威胁分析", "抗干扰中", "知识库", "推理引擎", "决策输出"]
# 创建3x2网格布局
grid_positions = [
(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2)
]
# 随机打乱位置
random.shuffle(grid_positions)
modules = []
for i in range(6):
# 创建模块框架
cell = ttk.Frame(grid_container)
cell.grid(row=grid_positions[i][0], column=grid_positions[i][1], padx=15, pady=15, sticky="nsew")
# 创建模块
module = CornerMarkModule(
cell,
width=220,
height=280,
module_id=i+1,
label_text=labels[i]
)
modules.append(module)
# 配置网格权重
grid_container.rowconfigure(grid_positions[i][0], weight=1)
grid_container.columnconfigure(grid_positions[i][1], weight=1)
# 启动所有模块的动画
for module in modules:
module.update_animation()
root.mainloop()
if __name__ == "__main__":
create_corner_marks()修改上面这段代码,不再采用2行3列排放,干扰放左上,抗干扰中放顶部,干扰右侧放威胁分析,威胁分析右侧放知识库和推理引擎,决策输出放最右边。其余功能不修改不增加,修改出完整代码