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 | class FluBadge(FluBadgeCanvas, DDrawWidget, FluToolTipBase):
def __init__(self, *args,
text="",
width=70,
height=30,
font=None,
mode="light",
style="standard",
**kwargs):
"""
初始化类
:param args: 参照tkinter.Canvas.__init__
:param text:
:param width:
:param height:
:param font:
:param mode: Fluent主题模式 分为 “light” “dark”
:param style:
:param kwargs: 参照tkinter.Canvas.__init__
"""
self._init(mode, style)
super().__init__(*args, width=width, height=height, **kwargs)
self.dconfigure(
text=text,
)
self.bind("<<Clicked>>", lambda event=None: self.focus_set(), add="+")
from .defs import set_default_font
set_default_font(font, self.attributes)
def _init(self, mode, style):
from easydict import EasyDict
self.attributes = EasyDict(
{
"text": "",
"command": None,
"font": None,
"back_color": None,
"back_opacity": None,
"border_color": None,
"border_color_opacity": None,
"border_width": None,
"text_color": None
}
)
self.theme(mode, style)
def _draw(self, event=None):
"""
重新绘制组件
:param event:
"""
super()._draw(event)
self.delete("all")
_back_color = self.attributes.back_color
_back_opacity = self.attributes.back_opacity
_border_color = self.attributes.border_color
_border_color_opacity = self.attributes.border_color_opacity
_border_width = self.attributes.border_width
_text_color = self.attributes.text_color
self.element_border = self.create_round_rectangle(
0, 0, self.winfo_width(), self.winfo_height(), temppath=self.temppath,
fill=_back_color, fill_opacity=_back_opacity,
outline=_border_color, outline_opacity=_border_color_opacity, width=_border_width
)
self.element_text = self.create_text(
self.winfo_width() / 2, self.winfo_height() / 2, anchor="center",
fill=_text_color, text=self.attributes.text, font=self.attributes.font
)
self.after(10, lambda: self.update())
def theme(self, mode=None, style=None):
if mode:
self.mode = mode
if style:
self.style = style
if self.mode.lower() == "dark":
if self.style.lower() == "accent":
self._dark_accent()
else:
self._dark()
else:
if self.style.lower() == "accent":
self._light_accent()
else:
self._light()
def _theme(self, mode, style):
n = badge(mode, style)
self.dconfigure(
back_color=n["back_color"],
back_opacity=n["back_opacity"],
border_color=n["border_color"],
border_color_opacity=n["border_color_opacity"],
border_width=n["border_width"],
text_color=n["text_color"],
)
def _light(self):
self._theme("light", "standard")
def _light_accent(self):
self._theme("light", "accent")
def _dark(self):
self._theme("dark", "standard")
def _dark_accent(self):
self._theme("dark", "accent")
|