完成按钮

This commit is contained in:
cxykevin 2024-03-24 14:33:59 +08:00
parent 909d242830
commit e4a2f35d6a
8 changed files with 589 additions and 27 deletions

View File

@ -1 +1,2 @@
from magictk import window
from magictk.window import Window
from magictk.button import Button, ButtonFill

483
magictk/button.py Normal file
View File

@ -0,0 +1,483 @@
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 Button:
color = color_tmpl.default_color
__fill_obj = []
__fill_func = []
__fill_gc = []
__fill_fc = []
__fill_hc = []
hover_mode = 0.0
__is_hover = 0
__flash_t = 0
max_flash = 4
__anim_obj_id = -1
__color_bd = "border_base"
__color_bg = "background"
__color_fg = "primary"
__color_fg1 = "primary_light"
__color_fg2 = "primary_light2"
text = "Button"
def __draw_corner(self, r_x, r_y, x, y, **kwargs):
border_info = json.loads(photoload.loadres("buttonborder"))
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+4-x_n-1
if (r_y == 0):
py = y+y_n+1
else:
py = y+4-y_n-1
if (j < 0):
lcolor = -j
else:
lcolor = j
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_fg2], 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_fg], int((1-lcolor/255)*1000)/1000)
else:
h_color = color_tmpl.mix_color(
self.color["background"], self.color[self.__color_fg], 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.__is_hover == 2):
self.canvas.itemconfig(
obj, fill=h_color)
else:
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 __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, w=80, h=30, text="Button", color_list: None | dict = None):
set_font()
self.w = max(80, w)
self.h = max(30, h)
self.text = text
self.__master = master
if (color_list is not None):
self.color = color_list
if (root_anim == None):
self.__root = master
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()
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, self.w-4, 0)
self.__draw_corner(0, 1, 0, self.h-5)
self.__draw_corner(1, 1, self.w-4, self.h-5)
def update_color(obj, g_color, f_color, h_color):
if (self.__is_hover == 2):
self.canvas.itemconfig(
obj, fill=h_color)
else:
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_fg])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
1, 5, 1, self.h-5, 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_fg])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
5, 1, self.w-4, 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_fg])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
self.w-1, 5, self.w-1, self.h-5, 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_fg])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.canvas.create_line(
5, self.h-2, self.w-4, self.h-2, width=1, fill=self.color[self.__color_fg1]))
self.__fill_fc.append(self.color[self.__color_fg2])
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, 5, self.w-1, self.h-5, width=0, fill=self.color[self.__color_fg2]))
self.__fill_fc.append(self.color[self.__color_fg2])
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(
5, 2, self.w-4, self.h-2, width=0, fill=self.color[self.__color_fg2]))
self.__fill_fc.append(self.color[self.__color_fg])
self.__fill_gc.append(self.color["regular_text"])
self.__fill_hc.append(self.color[self.__color_fg])
self.__fill_func.append(update_color)
self.__fill_obj.append(
self.canvas.create_text(int(self.w/2), int(self.h/2), text=self.text, font=(use_font, 10)))
def bind_anim(self):
def anim_magictk():
if (self.__is_hover == 1 and self.__flash_t < self.max_flash):
self.__flash_t += (1 if (len(self.__root.anim) > 6) else 1)
self.__flash_t = min(self.__flash_t, self.max_flash)
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
elif (self.__is_hover == 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.__is_hover == 1 and self.__flash_t < self.max_flash):
self.__flash_t += 1
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
elif (self.__is_hover == 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 enter_v(*args):
# self.bind_anim()
if (self.__is_hover == 0):
self.__is_hover = 1
self.canvas.bind("<Enter>", enter_v)
def leave_v(*args):
# self.bind_anim()
if (self.__is_hover == 1):
self.__is_hover = 0
self.canvas.bind("<Leave>", leave_v)
def press_v(*args):
self.__is_hover = 2
self.__update_color()
self.canvas.bind("<Button-1>", press_v)
def pressrelease_v(*args):
# self.bind_anim()
self.__is_hover = 1
self.__update_color()
self.canvas.bind("<ButtonRelease-1>", pressrelease_v)
class ButtonFill(Button):
__color_bd = "primary"
__color_bg = "primary"
__color_fg = "primary"
__color_fg1 = "primary_light3"
__color_fg2 = "primary_dark"
color = color_tmpl.default_color
__fill_obj = []
__fill_func = []
__fill_gc = []
__fill_fc = []
__fill_hc = []
hover_mode = 0.0
__is_hover = 0
__flash_t = 0
max_flash = 4
__anim_obj_id = -1
text = "ButtonFill"
def __draw_corner(self, r_x, r_y, x, y, **kwargs):
border_info = json.loads(photoload.loadres("buttonborder"))
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+4-x_n-1
if (r_y == 0):
py = y+y_n+1
else:
py = y+4-y_n-1
if (j < 0):
lcolor = -j
else:
lcolor = 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.__is_hover == 2):
self.canvas.itemconfig(
obj, fill=h_color)
else:
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 __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=30, text="Button", color_list: None | dict = None):
set_font()
self.__color_bd = color_type
self.__color_bg = color_type
self.__color_fg = color_type
self.__color_fg1 = color_type+"_light3"
self.__color_fg2 = color_type+"_dark"
self.w = max(80, w)
self.h = max(30, h)
self.text = text
self.__master = master
if (color_list is not None):
self.color = color_list
if (root_anim == None):
self.__root = master
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()
def __draw(self):
self.__draw_corner(0, 0, 0, 0)
self.__draw_corner(1, 0, self.w-4, 0)
self.__draw_corner(0, 1, 0, self.h-5)
self.__draw_corner(1, 1, self.w-4, self.h-5)
def update_color(obj, g_color, f_color, h_color):
if (self.__is_hover == 2):
self.canvas.itemconfig(
obj, fill=h_color)
else:
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, 5, 1, self.h-5, 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(
5, 1, self.w-4, 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(
self.w-1, 5, self.w-1, self.h-5, 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(
5, self.h-2, self.w-4, self.h-2, 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_rectangle(
2, 5, self.w-1, self.h-5, width=0, fill=self.color[self.__color_fg2]))
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_rectangle(
5, 2, self.w-4, self.h-2, width=0, fill=self.color[self.__color_fg2]))
self.__fill_fc.append("#FFFFFF")
self.__fill_gc.append("#FFFFFF")
self.__fill_hc.append("#FFFFFF")
self.__fill_func.append(update_color)
self.__fill_obj.append(
self.canvas.create_text(int(self.w/2), int(self.h/2), text=self.text, font=(use_font, 10)))
def bind_anim(self):
def anim_magictk():
if (self.__is_hover == 1 and self.__flash_t < self.max_flash):
self.__flash_t += (1 if (len(self.__root.anim) > 6) else 1)
self.__flash_t = min(self.__flash_t, self.max_flash)
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
elif (self.__is_hover == 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.__is_hover == 1 and self.__flash_t < self.max_flash):
self.__flash_t += 1
self.hover_mode = self.__flash_t/self.max_flash
self.__update_color()
elif (self.__is_hover == 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 enter_v(*args):
# self.bind_anim()
if (self.__is_hover == 0):
self.__is_hover = 1
self.canvas.bind("<Enter>", enter_v)
def leave_v(*args):
# self.bind_anim()
if (self.__is_hover == 1):
self.__is_hover = 0
self.canvas.bind("<Leave>", leave_v)
def press_v(*args):
self.__is_hover = 2
self.__update_color()
self.canvas.bind("<Button-1>", press_v)
def pressrelease_v(*args):
# self.bind_anim()
self.__is_hover = 1
self.__update_color()
self.canvas.bind("<ButtonRelease-1>", pressrelease_v)

View File

@ -2,10 +2,30 @@ import colorsys
default_color = {
"primary": "#409EFF",
"primary_light": "#C6E2FF",
"primary_light2": "#ECF5FF",
"primary_light3": "#79BBFF",
"primary_dark": "#337ECC",
"success": "#67C23A",
"success_light": "#E1F3D8",
"success_light2": "#F0F9EB",
"success_light3": "#95D475",
"success_dark": "#529B2E",
"warning": "#E6A23C",
"warning_light": "#FAECD8",
"warning_light2": "#E6A23C",
"warning_light3": "#EEBE77",
"warning_dark": "#B88230",
"danger": "#F56C6C",
"danger_light": "#FDE2E2",
"danger_light2": "#FEF0F0",
"danger_light3": "#F89898",
"danger_dark": "#C45656",
"info": "#909399",
"info_light": "#E9E9EB",
"info_light2": "#F4F4F5",
"info_light3": "#B1B3B8",
"info_dark": "#73767A",
"primary_text": "#303133",
"regular_text": "#606266",
"secondary_text": "#909399",

View File

@ -22,3 +22,10 @@ def loadimg(imgid: str):
load_cache[imgid] = tkinter.PhotoImage(
data=base64.b64decode(image_all[imgid]))
return load_cache[imgid]
def loadres(imgid: str):
global image_all
if imgid not in load_cache:
load_cache[imgid] = base64.b64decode(image_all[imgid])
return load_cache[imgid]

Binary file not shown.

View File

@ -23,7 +23,7 @@ try:
except ImportError:
from magictk import photoload
WIN_INF = 10000
WIN_INF = -10000
class Window(ttk.Frame):
@ -173,11 +173,22 @@ class Window(ttk.Frame):
self.__fake_tk.title(self.title)
self.main_tk.title(self.title)
self.__fake_tk.protocol("WM_DELETE_WINDOW", self.quit)
self.__fake_tk.bind('<FocusIn>', self.__top_windwow)
self.__fake_tk.bind('<FocusIn>', self.__top_window)
self.__fake_tk.resizable(0, 0)
self.__init_weights(self.main_tk)
self.main_tk.iconbitmap(os.path.dirname(__file__)+os.sep+"icon.ico")
self.__fake_tk.iconbitmap(os.path.dirname(__file__)+os.sep+"icon.ico")
try:
self.main_tk.iconbitmap(
os.path.dirname(__file__)+os.sep+"icon.ico")
self.__fake_tk.iconbitmap(
os.path.dirname(__file__)+os.sep+"icon.ico")
except:
# What's fuck in Linux
self.zoom()
self.main_tk.update()
self.__fake_tk.update()
self.zoom()
self.main_tk.update()
self.__fake_tk.update()
def mainloop(self) -> None:
t_start = time.time()
@ -186,7 +197,11 @@ class Window(ttk.Frame):
if (delta_t > 0.02): # flash animation
t_start = time.time()
for i in self.anim:
if (i is not None):
i()
self.anim = [i for i in self.anim if i is not None]
else:
pass
if (self.__fake_tk.state() == "iconic"):
if (self.normal_x == -1):
self.normal_x = self.x
@ -218,7 +233,7 @@ class Window(ttk.Frame):
self.zoom()
self.__fake_tk.iconify()
def __top_windwow(self, event=None) -> None:
def __top_window(self, event=None) -> None:
self.main_tk.state("normal")
self.main_tk.focus_set()
self.main_tk.attributes('-topmost', 'true')
@ -253,7 +268,7 @@ class Window(ttk.Frame):
self.update_size()
self.fullscreen = True
self.minmode = False
self.__top_windwow()
self.__top_window()
else:
self.normal_x = -1
self.x = self.old_x
@ -266,5 +281,5 @@ class Window(ttk.Frame):
self.fullscreen = False
self.minmode = False
self.update_size()
self.__top_windwow()
self.__top_window()
self.update_icon()

View File

@ -7,7 +7,6 @@ from typing import Optional, Tuple
def get_windows_workspace_size() -> Optional[Tuple[int, int, int, int]]:
try:
import win32api
import win32gui
# 获取工作区(不包括任务栏)的尺寸
work_area = win32api.GetMonitorInfo(
win32api.MonitorFromPoint((0, 0))).get('Work')
@ -22,21 +21,34 @@ def get_windows_workspace_size() -> Optional[Tuple[int, int, int, int]]:
return (0, 0, 1920, 1002)
def get_linux_workspace_size() -> Optional[Tuple[int, int, int, int]]:
def get_gtk_workspace_size() -> Optional[Tuple[int, int, int, int]]:
try:
output = subprocess.check_output(['xrandr']).decode('utf-8')
lines = output.split('\n')
primary_monitor = None
for line in lines:
if 'primary' in line:
primary_monitor = line.split()[-1]
if primary_monitor and primary_monitor in line:
dimensions = line.split()[0].split('x')
width = int(dimensions[0])
height = int(dimensions[1])
# xrandr does not provide the top-left corner directly, assuming (0, 0)
return 0, 0, width, height
except subprocess.CalledProcessError:
# if 1:
import gi
screen = gi.gdk.screen_get_default()
# 获取屏幕上显示器的数量
num_monitors = screen.get_n_monitors()
# 获取每个显示器的几何信息,并计算工作区的大小
workarea_width = 0
workarea_height = 0
for i in range(num_monitors):
monitor_geometry = screen.get_monitor_geometry(i)
monitor_x = monitor_geometry.x
monitor_y = monitor_geometry.y
monitor_width = monitor_geometry.width
monitor_height = monitor_geometry.height
workarea_width += monitor_width
workarea_height += monitor_height
print("工作区大小(宽度 x 高度):{} x {}".format(workarea_width, workarea_height))
except ImportError:
print("pygtk (PyGObject pygtk) library is not installed.")
except:
pass
return None
@ -61,8 +73,6 @@ def get_macos_workspace_size() -> Optional[Tuple[int, int, int, int]]:
def get_workspace_size() -> Optional[Tuple[int, int, int, int]]:
if sys.platform.startswith('win'):
return get_windows_workspace_size()
elif sys.platform.startswith('linux'):
return get_linux_workspace_size()
elif sys.platform.startswith('darwin'):
return get_macos_workspace_size()
return None
return get_gtk_workspace_size()

View File

@ -0,0 +1,26 @@
[
[
255,
205,
64,
14
],
[
205,
34,
-155,
-245
],
[
64,
-155,
-255,
-255
],
[
14,
-245,
-255,
-255
]
]