import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
import math
class CornerMarkModule:
def __init__(self, parent, width, height, module_id):
self.parent = parent
self.width = width
self.height = height
self.module_id = module_id
self.line_ids = []
# 创建画布
self.canvas = tk.Canvas(parent, width=width, height=height, bg="black", highlightthickness=0)
self.canvas.pack(side=LEFT, padx=10, pady=10)
# 绘制中心正方形 (80x80)
self.square_size = 80
self.start_x = width // 2 - self.square_size // 2 # 水平居中
self.start_y = height // 2 - self.square_size // 2 # 垂直居中
self.canvas.create_rectangle(
self.start_x, self.start_y,
self.start_x + self.square_size,
self.start_y + self.square_size,
fill="darkgray", # 正方形填充为深灰色
outline="darkgray" # 正方形轮廓为深灰色
)
# 添加模块编号标签
self.canvas.create_text(
width // 2, height - 15,
text=f"模块 {module_id}",
fill="darkgray",
font=("Arial", 10)
)
# 初始参数
self.base_gap = 12
self.base_length = 30
self.create_all_lines()
# 动画计数器
self.counter = 0
def create_all_lines(self):
# 左上角标记
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="darkgray"
))
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="darkgray"
))
# 右上角标记
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="darkgray"
))
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="darkgray"
))
# 左下角标记
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="darkgray"
))
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="darkgray"
))
# 右下角标记
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="darkgray"
))
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="darkgray"
))
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)
# 继续动画循环
self.parent.after(50, self.update_animation)
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="动态距离直角标记 (5模块)", themename="darkly")
root.configure(bg="black")
# 设置窗口大小并居中
window_width = 1000
window_height = 300
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}")
# 创建容器框架
container = ttk.Frame(root, bootstyle="dark")
container.pack(fill=BOTH, expand=YES, padx=20, pady=20)
# 创建5个直角标记模块
modules = []
for i in range(1, 6): # 创建5个模块
module = CornerMarkModule(container, width=180, height=250, module_id=i)
modules.append(module)
# 启动所有模块的动画
for module in modules:
module.update_animation()
root.mainloop()
if __name__ == "__main__":
create_corner_marks()现在需要对模块中心区域贴上标签,分别是,受到干扰,干扰类型,对抗中,知识库,推理中,决策生成。
最新发布