-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_components.py
More file actions
184 lines (152 loc) · 5.51 KB
/
ui_components.py
File metadata and controls
184 lines (152 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import customtkinter as ctk
import tkinter as tk
class UIBuilder:
"""UI组件构建辅助类"""
# 颜色主题
PRIMARY_COLOR = "#1f6feb"
SUCCESS_COLOR = "#2ea043"
WARNING_COLOR = "#d29922"
ERROR_COLOR = "#f85149"
CARD_BG = "#1c1c1e"
@staticmethod
def create_card(parent, title):
"""创建卡片容器"""
card = ctk.CTkFrame(parent, fg_color=UIBuilder.CARD_BG, corner_radius=15)
title_label = ctk.CTkLabel(
card, text=title, font=ctk.CTkFont(size=14, weight="bold"), anchor="w"
)
title_label.pack(fill="x", padx=15, pady=(15, 10))
return card
@staticmethod
def create_labeled_entry(parent, label_text, var, width=120):
"""创建带标签的输入框"""
frame = ctk.CTkFrame(parent, fg_color="transparent")
frame.pack(fill="x", pady=(0, 10))
ctk.CTkLabel(
frame,
text=label_text,
font=ctk.CTkFont(size=12, weight="bold"),
width=width,
anchor="w",
).pack(side="left", padx=(0, 10))
entry = ctk.CTkEntry(frame, textvariable=var, corner_radius=8, height=35)
entry.pack(side="left", fill="x", expand=True)
return frame, entry
@staticmethod
def create_file_picker(parent, label_text, var, command, button_text="选择"):
"""创建文件/文件夹选择器"""
frame = ctk.CTkFrame(parent, fg_color="transparent")
frame.pack(fill="x", pady=(0, 10))
ctk.CTkLabel(
frame,
text=label_text,
font=ctk.CTkFont(size=12, weight="bold"),
width=120,
anchor="w",
).pack(side="left", padx=(0, 10))
ctk.CTkEntry(frame, textvariable=var, corner_radius=8, height=35).pack(
side="left", fill="x", expand=True, padx=(0, 10)
)
ctk.CTkButton(
frame,
text=button_text,
command=command,
corner_radius=8,
width=120,
fg_color="#404044",
hover_color="#505054",
).pack(side="left")
return frame
@staticmethod
def create_param_card(parent, title, icon):
"""创建参数卡片容器"""
container = ctk.CTkFrame(parent, fg_color="transparent")
frame = ctk.CTkFrame(container, fg_color="#2a2a2d", corner_radius=10)
frame.pack(fill="both", expand=True)
ctk.CTkLabel(
frame, text=f"{icon} {title}", font=ctk.CTkFont(size=12, weight="bold")
).pack(anchor="w", padx=15, pady=(10, 5))
return container, frame
@staticmethod
def create_combobox(parent, variable, values, **kwargs):
"""创建下拉框"""
combo = ctk.CTkComboBox(
parent,
variable=variable,
values=values,
state="readonly",
corner_radius=8,
button_color=UIBuilder.PRIMARY_COLOR,
button_hover_color="#1557c0",
dropdown_fg_color="#2a2a2d",
dropdown_hover_color="#3a3a3d",
dropdown_text_color="white",
**kwargs,
)
return combo
@staticmethod
def create_slider_with_label(parent, variable, from_, to, command, label_var=None):
"""创建带数值显示的滑块"""
inner = ctk.CTkFrame(parent, fg_color="transparent")
inner.pack(fill="x", padx=15, pady=(0, 10))
label = ctk.CTkLabel(
inner,
text=str(int(variable.get())),
font=ctk.CTkFont(size=11, weight="bold"),
text_color=UIBuilder.PRIMARY_COLOR,
width=30,
)
label.pack(side="right", padx=(5, 0))
slider = ctk.CTkSlider(
inner,
from_=from_,
to=to,
number_of_steps=int(to - from_),
variable=variable,
command=command,
button_color=UIBuilder.PRIMARY_COLOR,
button_hover_color="#1557c0",
progress_color=UIBuilder.PRIMARY_COLOR,
)
slider.pack(side="left", fill="x", expand=True)
return slider, label
@staticmethod
def create_textbox(parent, **kwargs):
"""创建文本框"""
defaults = {
"wrap": tk.WORD,
"corner_radius": 10,
"font": ctk.CTkFont(family="Consolas", size=11),
"fg_color": "#1a1a1c",
}
defaults.update(kwargs)
textbox = ctk.CTkTextbox(parent, **defaults)
return textbox
class AnimationHelper:
"""动画效果辅助类"""
@staticmethod
def animate_button_click(button, original_height=45):
"""按钮点击动画 - 缩放效果"""
def scale_down():
button.configure(height=original_height - 5)
button.master.after(50, scale_up)
def scale_up():
button.configure(height=original_height)
scale_down()
@staticmethod
def animate_progress_completion(progress_bar, colors=None):
"""进度条完成动画 - 闪烁效果"""
if colors is None:
colors = [
UIBuilder.PRIMARY_COLOR,
UIBuilder.SUCCESS_COLOR,
UIBuilder.PRIMARY_COLOR,
UIBuilder.SUCCESS_COLOR,
]
def flash(step=0):
if step < len(colors):
progress_bar.configure(progress_color=colors[step])
progress_bar.master.after(150, lambda: flash(step + 1))
else:
progress_bar.configure(progress_color=UIBuilder.SUCCESS_COLOR)
flash()