添加单/多选框

This commit is contained in:
cxykevin 2024-04-07 21:37:14 +08:00
parent 1ea843e53d
commit 913c4a3496
4 changed files with 452 additions and 0 deletions

319
magictk/checkbox.py Normal file
View File

@ -0,0 +1,319 @@
import json
import tkinter
from tkinter import ttk
from tkinter import font as tkfont
try:
import color_tmpl
except ImportError:
from magictk import color_tmpl
try:
import photoload
except ImportError:
from magictk import photoload
def set_font():
global use_font
font_family = ["Helvetica Neue", "Helvetica", "PingFang SC", "Hiragino Sans GB",
"Microsoft YaHei",
"微软雅黑",
"Arial", "sans-serif"]
t_family = tkfont.families(root=None, displayof=None)
for i in font_family:
if i in t_family:
use_font = i
break
else:
print("Unknown font")
class Checkbox:
__color_bd = "border_base"
__color_bg = "background"
__color_fg = "primary"
__color_fg1 = "primary"
__color_fg2 = "primary_dark"
color = color_tmpl.default_color
__fill_obj = []
__fill_func = []
__fill_gc = []
__fill_fc = []
__fill_hc = []
hover_mode = 0.0
ishover = 0
__flash_t = 0
max_flash = 4
__anim_obj_id = -1
checked = False
text = "ButtonFill"
_group_id = 0
disable_unhover = 0
def callback(*args): return None
def __draw_corner(self, r_x, r_y, x, y, **kwargs):
border_info = json.loads(photoload.loadres("checkboxborder"))
y_n = 0
for i in border_info:
x_n = 0
for j in i:
if (r_x == 0):
px = x+x_n+1
else:
px = x+2-x_n-1
if (r_y == 0):
py = y+y_n+1
else:
py = y+2-y_n-1
if (j < 0):
lcolor = 255+j
else:
lcolor = 255-j
if (j < 0):
g_color = color_tmpl.mix_color(
self.color[self.__color_bg], self.color[self.__color_bd], int((1-lcolor/255)*1000)/1000)
else:
g_color = color_tmpl.mix_color(
self.color["background"], self.color[self.__color_bd], int((1-lcolor/255)*1000)/1000)
if (j < 0):
f_color = color_tmpl.mix_color(
self.color[self.__color_fg1], self.color[self.__color_fg1], int((1-lcolor/255)*1000)/1000)
else:
f_color = color_tmpl.mix_color(
self.color["background"], self.color[self.__color_fg1], int((1-lcolor/255)*1000)/1000)
if (j < 0):
h_color = color_tmpl.mix_color(
self.color[self.__color_fg2], self.color[self.__color_fg2], int((1-lcolor/255)*1000)/1000)
else:
h_color = color_tmpl.mix_color(
self.color["background"], self.color[self.__color_fg2], int((1-lcolor/255)*1000)/1000)
obj = self.canvas.create_rectangle(
px, py, px, py, width=0, fill=g_color)
def update_color(obj, g_color, f_color, h_color):
if (self.ishover == 2):
self.canvas.itemconfig(
obj, fill=h_color)
elif (self.__flash_t <= self.max_flash):
self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode))
self.__fill_func.append(update_color)
self.__fill_gc.append(g_color)
self.__fill_fc.append(f_color)
self.__fill_hc.append(h_color)
self.__fill_obj.append(obj)
x_n += 1
y_n += 1
def __draw_img(self, x, y, **kwargs):
border_info = json.loads(photoload.loadres("checkbox"))
y_n = 0
for i in border_info:
x_n = 0
for j in i:
px = x+x_n+1
py = y+y_n+1
if (j < 0):
lcolor = 255+j
else:
lcolor = 255-j
g_color = self.color[self.__color_fg]
f_color = color_tmpl.mix_color(
self.color["background"], self.color[self.__color_fg], int((1-lcolor/255)*1000)/1000)
h_color = color_tmpl.mix_color(
self.color["background"], self.color[self.__color_fg2], int((1-lcolor/255)*1000)/1000)
obj = self.canvas.create_rectangle(
px, py, px, py, width=0, fill=g_color)
def update_color(obj, g_color, f_color, h_color):
if (self.__flash_t > self.max_flash):
self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(g_color, f_color, (self.__flash_t-self.max_flash)/self.max_flash))
else:
self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(self.color["background"], self.color[self.__color_fg1], self.hover_mode))
self.__fill_func.append(update_color)
self.__fill_gc.append(g_color)
self.__fill_fc.append(f_color)
self.__fill_hc.append(h_color)
self.__fill_obj.append(obj)
x_n += 1
y_n += 1
def __update_color(self):
n = 0
for i in self.__fill_func:
i(self.__fill_obj[n], self.__fill_gc[n],
self.__fill_fc[n], self.__fill_hc[n])
n += 1
def __init__(self, master=None, root_anim=None, color_type="primary", w=80, h=16, text="Button", color_list: None | dict = None, group: None = None):
set_font()
self.w = max(80, w)
self.h = 16
self.text = text
self.__master = master
if (color_list is not None):
self.color = color_list
if (root_anim == None):
self.root = master.root
else:
self.root = root_anim
self.canvas = tkinter.Canvas(
master, bg=self.color["background"], width=self.w, height=self.h, borderwidth=0, bd=0, highlightcolor=self.color["background"], highlightthickness=0)
self.__draw()
self.__update_color()
self.__bind_event()
self.bind_anim()
if (group is not None):
group._add_checkbox(self)
def pack(self, *args, **kwargs):
self.canvas.pack(*args, **kwargs)
def guid(self, *args, **kwargs):
self.canvas.guid(*args, **kwargs)
def place(self, *args, **kwargs):
self.canvas.place(*args, **kwargs)
def __draw(self):
self.__draw_corner(0, 0, 0, 0)
self.__draw_corner(1, 0, 12, 0)
self.__draw_corner(0, 1, 0, 12)
self.__draw_corner(1, 1, 12, 12)
def update_color(obj, g_color, f_color, h_color):
if (self.ishover == 2):
self.canvas.itemconfig(
obj, fill=h_color)
elif (self.__flash_t <= self.max_flash):
self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode))
self.__fill_fc.append(self.color[self.__color_fg1])
self.__fill_gc.append(self.color[self.__color_bd])
self.__fill_hc.append(self.color[self.__color_fg2])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
1, 3, 1, 12, width=1, fill=self.color[self.__color_bd]))
self.__fill_fc.append(self.color[self.__color_fg1])
self.__fill_gc.append(self.color[self.__color_bd])
self.__fill_hc.append(self.color[self.__color_fg2])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
3, 1, 12, 1, width=1, fill=self.color[self.__color_fg1]))
self.__fill_fc.append(self.color[self.__color_fg1])
self.__fill_gc.append(self.color[self.__color_bd])
self.__fill_hc.append(self.color[self.__color_fg2])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
13, 3, 13, 12, width=1, fill=self.color[self.__color_fg1]))
self.__fill_fc.append(self.color[self.__color_fg1])
self.__fill_gc.append(self.color[self.__color_bd])
self.__fill_hc.append(self.color[self.__color_fg2])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
3, 13, 12, 13, width=1, fill=self.color[self.__color_fg1]))
self.__fill_fc.append(self.color[self.__color_fg1])
self.__fill_gc.append(self.color["background"])
self.__fill_hc.append(self.color[self.__color_fg2])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_rectangle(
2, 3, 13, 12, width=0, fill=self.color[self.__color_fg2]))
self.__fill_fc.append(self.color[self.__color_fg1])
self.__fill_gc.append(self.color["background"])
self.__fill_hc.append(self.color[self.__color_fg2])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_rectangle(
3, 2, 12, 13, width=0, fill=self.color[self.__color_fg2]))
self.__draw_img(2, 2)
self.__fill_fc.append(self.color[self.__color_fg])
self.__fill_gc.append(self.color["regular_text"])
self.__fill_hc.append("#FFFFFF")
self.__fill_func.append(update_color)
self.__fill_obj.append(
self.canvas.create_text(14+int((self.w-14)/2), 6, text=self.text, font=(use_font, 10)))
def bind_anim(self):
def anim_magictk():
if (self.ishover == 1 and self.__flash_t < self.max_flash*2):
self.__flash_t += (1 if (len(self.root.anim) > 6) else 1)
self.__flash_t = min(self.__flash_t, self.max_flash*2)
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
elif (self.ishover == 0 and self.__flash_t > 0):
self.__flash_t -= (1 if (len(self.root.anim) > 6) else 1)
self.__flash_t = max(self.__flash_t, 0)
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
# elif (self.__is_hover == 0 and self.__flash_t <= 0):
# if self.__anim_obj_id != -1:
# self.__anim_obj_id = None
def anim_normal(*args):
if (self.ishover == 1 and self.__flash_t < self.max_flash*2):
self.__flash_t += 1
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
elif (self.ishover == 0 and self.__flash_t > 0):
self.__flash_t -= 1
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
self.root.after(anim_normal, 16)
try:
self.root.anim == 0
except:
self.root.after(anim_normal, 16)
else:
if (anim_magictk not in self.root.anim):
self.root.anim.append(anim_magictk)
self.__anim_obj_id = self.root.anim[-1]
def __bind_event(self):
def press_v(*args):
if (self.ishover == 1):
if (self.disable_unhover == 0):
self.ishover = 0
else:
self.ishover = 1
self.callback(self)
self.__update_color()
self.canvas.bind("<Button-1>", press_v)
def pressrelease_v(*args):
self.__update_color()
self.canvas.bind("<ButtonRelease-1>", pressrelease_v)
class RadioGroup:
value = 1
__g_list = []
def __change(self, btn):
for i in self.__g_list:
self.value = btn._group_id
if (i != btn):
i.ishover = 0
else:
i.ishover = 1
def _add_checkbox(self, cb: Checkbox):
self.__g_list.append(cb)
cb.callback = self.__change
cb._group_id = len(self.__g_list)
cb.max_flash = 3
cb.disable_unhover = 1
if (len(self.__g_list) == 1):
cb.ishover = 1

View File

@ -30,6 +30,7 @@ default_color = {
"regular_text": "#606266", "regular_text": "#606266",
"secondary_text": "#909399", "secondary_text": "#909399",
"placeholder": "#C0C4CC", "placeholder": "#C0C4CC",
"placeholder_light": "#F5F7FA",
"border_base": "#DCDFE6", "border_base": "#DCDFE6",
"border_light": "#E4E7ED", "border_light": "#E4E7ED",
"background": "#FFFFFF" "background": "#FFFFFF"

122
photo2/res/checkbox.json Normal file
View File

@ -0,0 +1,122 @@
[
[
255,
255,
255,
255,
255,
255,
255,
255,
224,
255
],
[
255,
255,
255,
255,
255,
255,
255,
255,
224,
255
],
[
255,
255,
255,
255,
255,
255,
255,
201,
0,
193
],
[
255,
255,
255,
255,
255,
255,
201,
0,
158,
255
],
[
255,
180,
255,
255,
255,
201,
0,
158,
255,
255
],
[
193,
0,
170,
255,
201,
0,
158,
255,
224,
255
],
[
255,
189,
0,
134,
0,
158,
255,
255,
224,
255
],
[
255,
255,
166,
0,
112,
255,
255,
255,
224,
255
],
[
255,
255,
255,
146,
255,
255,
255,
255,
224,
255
],
[
255,
255,
255,
255,
255,
255,
255,
255,
224,
255
]
]

View File

@ -0,0 +1,10 @@
[
[
98,
243
],
[
243,
-255
]
]