完成icon

This commit is contained in:
cxykevin 2024-04-26 09:00:26 +08:00
parent 8ca2a6a5d1
commit f53c349ea4
5 changed files with 184 additions and 36 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.pack filter=lfs diff=lfs merge=lfs -text

View File

@ -2,8 +2,8 @@ import magictk
import random
from multiprocessing import Process
if __name__ == "__main__":
mwin = magictk.Window()
win = magictk.Container(mwin, h=10000, container_h=500)
win = magictk.Window()
# win = magictk.Container(mwin, h=10000, container_h=500)
frame = magictk.Frame(win)
magictk.Button(frame, text="Default",
@ -29,7 +29,7 @@ if __name__ == "__main__":
for i in range(1, 10):
obj1.addmenu(f"Option {i}", lambda s, t: print(i))
magictk.Button(frame2, text="Menu",
func=lambda s: magictk.Menu(mwin, menuobj=obj1), w=60).pack()
func=lambda s: magictk.Menu(win, menuobj=obj1), w=60).pack()
frame2.pack()
frame3 = magictk.Frame(win)
@ -44,25 +44,36 @@ if __name__ == "__main__":
group=groups).pack(side='left')
frame3.pack()
frame4 = magictk.Frame(win)
magictk.Checkbox(frame4, text="Option 1", w=100).pack(side='left')
magictk.Checkbox(frame4, text="Option 2", w=100).pack(side='left')
frame4.pack()
frame5 = magictk.Frame(win)
pb = magictk.ProgressBar(frame5)
pb.pack(side='left')
magictk.Button(frame5, text="+",
func=lambda s: pb.add_progress(0.1), w=30).pack(side='left')
magictk.Button(frame5, text="-",
func=lambda s: pb.add_progress(-0.1), w=30).pack(side='left')
magictk.Button(frame5, text="++",
func=lambda s: pb.add_progress(0.3), w=40).pack(side='left')
magictk.Button(frame5, text="--",
func=lambda s: pb.add_progress(-0.3), w=40).pack(side='left')
magictk.Checkbox(frame5, text="Option 1", w=100).pack(side='left')
magictk.Checkbox(frame5, text="Option 2", w=100).pack(side='left')
frame5.pack()
frame4 = magictk.Frame(win)
magictk.Icon(frame4, size=12).pack(side='left')
magictk.Icon(frame4, size=16).pack(side='left')
magictk.Icon(frame4, size=32).pack(side='left')
magictk.Icon(frame4, size=48).pack(side='left')
magictk.Button(frame4,
func=lambda s: print("Icon Btn 1"), iconname="apple", iconsize=16, w=32).pack(side='left')
magictk.ButtonFill(frame4, color_type="primary",
func=lambda s: print("Icon Btn 2"), iconname="apple", iconsize=16, w=32).pack(side='left')
frame4.pack()
frame6 = magictk.Frame(win)
pb = magictk.ProgressBar(frame6)
pb.pack(side='left')
magictk.Button(frame6, text="+",
func=lambda s: pb.add_progress(0.1), w=30).pack(side='left')
magictk.Button(frame6, text="-",
func=lambda s: pb.add_progress(-0.1), w=30).pack(side='left')
magictk.Button(frame6, text="++",
func=lambda s: pb.add_progress(0.3), w=40).pack(side='left')
magictk.Button(frame6, text="--",
func=lambda s: pb.add_progress(-0.3), w=40).pack(side='left')
frame6.pack()
frame7 = magictk.Frame(win)
def kaoji():
def test(*args):
@ -78,30 +89,30 @@ if __name__ == "__main__":
del p
ts = Process(target=test)
ts.start()
magictk.Button(frame6, text="Performance Test",
magictk.Button(frame7, text="Performance Test",
func=lambda s: kaoji(), w=130).pack(side='left')
frame6.pack()
frame7 = magictk.Frame(win)
magictk.Entry(frame7, w=200).pack(side='left')
frame7.pack()
frame8 = magictk.Container(win, h=200, container_h=30*6*5)
frame8 = magictk.Frame(win)
magictk.Entry(frame8, w=200).pack(side='left')
frame8.pack()
frame9 = magictk.Container(win, h=200, container_h=30*6*5)
for i in range(5):
magictk.Button(frame8, text="Default",
magictk.Button(frame9, text="Default",
func=lambda s: print("Btn 1")).pack()
magictk.ButtonFill(frame8, text="Primary",
magictk.ButtonFill(frame9, text="Primary",
func=lambda s: print("Btn 2")).pack()
magictk.ButtonFill(frame8, color_type="success", text="Success",
magictk.ButtonFill(frame9, color_type="success", text="Success",
func=lambda s: print("Btn 3")).pack()
magictk.ButtonFill(frame8, color_type="info", text="Info",
magictk.ButtonFill(frame9, color_type="info", text="Info",
func=lambda s: print("Btn 4")).pack()
magictk.ButtonFill(frame8, color_type="warning", text="Warning",
magictk.ButtonFill(frame9, color_type="warning", text="Warning",
func=lambda s: print("Btn 5")).pack()
magictk.ButtonFill(frame8, color_type="danger", text="Danger",
magictk.ButtonFill(frame9, color_type="danger", text="Danger",
func=lambda s: print("Btn 6")).pack()
frame8.pack(expand=True, fill="x")
frame9.pack(expand=True, fill="x")
win.pack(expand=True, fill="x", anchor="n")
# win.pack(expand=True, fill="x", anchor="n")
mwin.mainloop()
win.mainloop()

View File

@ -8,3 +8,4 @@ from magictk.select import Select
from magictk.frame import Frame, Container
from magictk.entry import Entry
from magictk.scrollbar import ScrollBar
from magictk.icon import Icon

View File

@ -68,6 +68,42 @@ class Button:
x_n += 1
y_n += 1
def _draw_icon(self, x, y, name, size, **kwargs):
border_info = border_info = json.loads(photoload.loadres(
f"icon/{name}@{size}"))
y_n = 0
for i in border_info:
x_n = 0
for j in i:
px = x+y_n+1
py = y+x_n+1
lcolor = j
g_color = color_tmpl.mix_color(
self.color["regular_text"], self.color["background"], int((1-lcolor/255)*1000)/1000)
f_color = color_tmpl.mix_color(
self.color[self._color_fg], self.color[self._color_fg2], int((1-lcolor/255)*1000)/1000)
h_color = color_tmpl.mix_color(
self.color[self._color_fg], 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:
@ -75,7 +111,7 @@ class Button:
self._fill_fc[n], self._fill_hc[n])
n += 1
def __init__(self, master=None, root_anim=None, w=80, h=30, text="Button", func=lambda s: print("Press"), color_list: dict = None, _set_defaultcolor=None):
def __init__(self, master=None, root_anim=None, w=80, h=30, text="Button", func=lambda s: print("Press"), color_list: dict = None, _set_defaultcolor=None, iconname="", iconsize=24):
global use_font
use_font = fontconfig.getfont()
self._fill_obj = []
@ -107,7 +143,13 @@ class Button:
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)
if (iconname != ''):
self.text = ""
self._draw()
if (iconname != ''):
self.size = iconsize
self._draw_icon(self.w//2-self.size//2, self.h //
2-self.size//2, iconname, iconsize)
self._update_color()
self._bind_event()
self.bind_anim()
@ -252,6 +294,42 @@ class Button:
class ButtonFill(Button):
def _draw_icon(self, x, y, name, size, **kwargs):
border_info = border_info = json.loads(photoload.loadres(
f"icon/{name}@{size}"))
y_n = 0
for i in border_info:
x_n = 0
for j in i:
px = x+y_n+1
py = y+x_n+1
lcolor = j
g_color = color_tmpl.mix_color(
"#FFFFFF", self.color[self._color_bd], int((1-lcolor/255)*1000)/1000)
f_color = color_tmpl.mix_color(
"#FFFFFF", self.color[self._color_fg1], int((1-lcolor/255)*1000)/1000)
h_color = color_tmpl.mix_color(
"#FFFFFF", 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 _draw_corner(self, r_x, r_y, x, y, **kwargs):
border_info = json.loads(photoload.loadres("buttonborder"))
y_n = 0
@ -308,7 +386,7 @@ class ButtonFill(Button):
x_n += 1
y_n += 1
def __init__(self, master=None, root_anim=None, color_type="primary", w=80, h=30, text="Button", func=lambda s: print("Press"), color_list: dict = None, _dis_color=None):
def __init__(self, master=None, root_anim=None, color_type="primary", w=80, h=30, text="Button", func=lambda s: print("Press"), color_list: dict = None, _dis_color=None, iconname="", iconsize=24):
if (_dis_color is None):
self._color_bd = color_type
self._color_bg = color_type
@ -319,4 +397,4 @@ class ButtonFill(Button):
self._color_fg2 = color_type+"_dark"
self._color_text = "#FFFFFF"
super().__init__(master=master, root_anim=root_anim, w=w, h=h,
text=text, color_list=color_list, func=func, _set_defaultcolor=True)
text=text, color_list=color_list, func=func, _set_defaultcolor=True, iconname=iconname, iconsize=iconsize)

57
magictk/icon.py Normal file
View File

@ -0,0 +1,57 @@
import json
import tkinter
from tkinter import ttk
from magictk import color_tmpl
from magictk import photoload
from magictk import fontconfig
class Icon:
color = color_tmpl.default_color
size = 12
iconname = "apple"
setcolor = "primary"
def _draw(self, x, y, **kwargs):
border_info = json.loads(photoload.loadres(
f"icon/{self.iconname}@{self.size}"))
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 = -j
else:
lcolor = j
if (self.setcolor[0] == '#'):
g_color = color_tmpl.mix_color(
self.setcolor, self.color["background"], int((1-lcolor/255)*1000)/1000)
else:
g_color = color_tmpl.mix_color(
self.color[self.setcolor], self.color["background"], int((1-lcolor/255)*1000)/1000)
obj = self.canvas.create_rectangle(
py, px, py, px, width=0, fill=g_color)
x_n += 1
y_n += 1
def __init__(self, master=None, size=16, iconname="apple", setcolor="regular_text", color_list: dict = None, _set_defaultcolor=None):
self.size = size
self.iconname = iconname
self.setcolor = setcolor
self.canvas = tkinter.Canvas(
master, bg=self.color["background"], width=size+2, height=size+2, borderwidth=0, bd=0, highlightcolor=self.color["background"], highlightthickness=0)
self._draw(0, 0)
def pack(self, *args, **kwargs):
self.canvas.pack(*args, **kwargs)
def grid(self, *args, **kwargs):
self.canvas.grid(*args, **kwargs)
def place(self, *args, **kwargs):
self.canvas.place(*args, **kwargs)