Compare commits

..

No commits in common. "19ee0bb25eaa1c3575634bc736392e21f616be0b" and "fd1dd154fc668c50e9741e704fdffce69471610b" have entirely different histories.

5 changed files with 323 additions and 450 deletions

View File

@ -81,8 +81,7 @@ if __name__ == "__main__":
func=lambda s: kaoji(), w=130).pack(side='left') func=lambda s: kaoji(), w=130).pack(side='left')
frame6.pack() frame6.pack()
frame7 = magictk.Frame(win)
magictk.Entry(frame7, w=100).pack(side='left')
frame7.pack()
win.mainloop() win.mainloop()
# from magictk import workspace
# print(workspace.get_gtk_workspace_size())

View File

@ -5,4 +5,3 @@ from magictk.checkbox import Checkbox, RadioGroup
from magictk.submenu import Menu, MenuObjs from magictk.submenu import Menu, MenuObjs
from magictk.select import Select from magictk.select import Select
from magictk.frame import Frame from magictk.frame import Frame
from magictk.entry import Entry

View File

@ -1,22 +1,52 @@
import json import json
import tkinter import tkinter
from tkinter import ttk 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
from magictk import color_tmpl
from magictk import photoload def set_font():
from magictk import fontconfig 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: class Button:
color = color_tmpl.default_color color = color_tmpl.default_color
__fill_obj = []
__fill_func = []
__fill_gc = []
__fill_fc = []
__fill_hc = []
hover_mode = 0.0 hover_mode = 0.0
_is_hover = 0 __is_hover = 0
_flash_t = 0 __flash_t = 0
max_flash = 4 max_flash = 4
_anim_obj_id = -1 __anim_obj_id = -1
__color_bd = "border_base"
__color_bg = "background"
__color_fg = "primary"
__color_fg1 = "primary_light"
__color_fg2 = "primary_light2"
text = "Button" text = "Button"
def _draw_corner(self, r_x, r_y, x, y, **kwargs): def __draw_corner(self, r_x, r_y, x, y, **kwargs):
border_info = json.loads(photoload.loadres("buttonborder")) border_info = json.loads(photoload.loadres("buttonborder"))
y_n = 0 y_n = 0
for i in border_info: for i in border_info:
@ -35,64 +65,49 @@ class Button:
else: else:
lcolor = j lcolor = j
g_color = color_tmpl.mix_color( g_color = color_tmpl.mix_color(
self.color["background"], self.color[self._color_bd], int((1-lcolor/255)*1000)/1000) self.color["background"], self.color[self.__color_bd], int((1-lcolor/255)*1000)/1000)
if (j < 0): if (j < 0):
f_color = color_tmpl.mix_color( f_color = color_tmpl.mix_color(
self.color[self._color_fg2], self.color[self._color_fg1], int((1-lcolor/255)*1000)/1000) self.color[self.__color_fg2], self.color[self.__color_fg1], int((1-lcolor/255)*1000)/1000)
else: else:
f_color = color_tmpl.mix_color( f_color = color_tmpl.mix_color(
self.color["background"], self.color[self._color_fg1], int((1-lcolor/255)*1000)/1000) self.color["background"], self.color[self.__color_fg1], int((1-lcolor/255)*1000)/1000)
if (j < 0): if (j < 0):
h_color = color_tmpl.mix_color( h_color = color_tmpl.mix_color(
self.color[self._color_fg2], self.color[self._color_fg], int((1-lcolor/255)*1000)/1000) self.color[self.__color_fg2], self.color[self.__color_fg], int((1-lcolor/255)*1000)/1000)
else: else:
h_color = color_tmpl.mix_color( h_color = color_tmpl.mix_color(
self.color["background"], self.color[self._color_fg], int((1-lcolor/255)*1000)/1000) self.color["background"], self.color[self.__color_fg], int((1-lcolor/255)*1000)/1000)
obj = self.canvas.create_rectangle( obj = self.canvas.create_rectangle(
px, py, px, py, width=0, fill=g_color) px, py, px, py, width=0, fill=g_color)
def update_color(obj, g_color, f_color, h_color): def update_color(obj, g_color, f_color, h_color):
if (self._is_hover == 2): if (self.__is_hover == 2):
self.canvas.itemconfig( self.canvas.itemconfig(
obj, fill=h_color) obj, fill=h_color)
else: else:
self.canvas.itemconfig( self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode)) obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode))
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_gc.append(g_color) self.__fill_gc.append(g_color)
self._fill_fc.append(f_color) self.__fill_fc.append(f_color)
self._fill_hc.append(h_color) self.__fill_hc.append(h_color)
self._fill_obj.append(obj) self.__fill_obj.append(obj)
x_n += 1 x_n += 1
y_n += 1 y_n += 1
def _update_color(self): def __update_color(self):
n = 0 n = 0
for i in self._fill_func: for i in self.__fill_func:
i(self._fill_obj[n], self._fill_gc[n], i(self.__fill_obj[n], self.__fill_gc[n],
self._fill_fc[n], self._fill_hc[n]) self.__fill_fc[n], self.__fill_hc[n])
n += 1 n += 1
def __init__(self, master=None, root_anim=None, w=80, h=30, text="Button", func=lambda s: print("Press"), color_list: None | 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: None | dict = None):
global use_font set_font()
use_font = fontconfig.getfont() self.__func = func
self._fill_obj = []
self._fill_func = []
self._fill_gc = []
self._fill_fc = []
self._fill_hc = []
self._func = func
if (_set_defaultcolor is None):
self._color_bd = "border_base"
self._color_bg = "background"
self._color_bg1 = "background"
self._color_fg = "primary"
self._color_fg1 = "primary_light"
self._color_fg2 = "primary_light2"
self._color_fg3 = "primary_light2"
self._color_text = None
self.w = max(30, w) self.w = max(30, w)
self.h = max(30, h) self.h = max(30, h)
self.text = text self.text = text
@ -107,8 +122,8 @@ class Button:
self.canvas = tkinter.Canvas( 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) master, bg=self.color["background"], width=self.w, height=self.h, borderwidth=0, bd=0, highlightcolor=self.color["background"], highlightthickness=0)
self._draw() self.__draw()
self._update_color() self.__update_color()
self.__bind_event() self.__bind_event()
self.bind_anim() self.bind_anim()
@ -121,94 +136,90 @@ class Button:
def place(self, *args, **kwargs): def place(self, *args, **kwargs):
self.canvas.place(*args, **kwargs) self.canvas.place(*args, **kwargs)
def _draw(self): def __draw(self):
self._draw_corner(0, 0, 0, 0) self.__draw_corner(0, 0, 0, 0)
self._draw_corner(1, 0, self.w-4, 0) self.__draw_corner(1, 0, self.w-4, 0)
self._draw_corner(0, 1, 0, self.h-5) self.__draw_corner(0, 1, 0, self.h-5)
self._draw_corner(1, 1, self.w-4, self.h-5) self.__draw_corner(1, 1, self.w-4, self.h-5)
def update_color(obj, g_color, f_color, h_color): def update_color(obj, g_color, f_color, h_color):
if (self._is_hover == 2): if (self.__is_hover == 2):
self.canvas.itemconfig( self.canvas.itemconfig(
obj, fill=h_color) obj, fill=h_color)
else: else:
self.canvas.itemconfig( self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode)) obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode))
self._fill_fc.append(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_gc.append(self.color[self.__color_bd])
self._fill_hc.append(self.color[self._color_fg]) self.__fill_hc.append(self.color[self.__color_fg])
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_obj.append(self.canvas.create_line( self.__fill_obj.append(self.canvas.create_line(
1, 5, 1, self.h-5, width=1, fill=self.color[self._color_bd])) 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_fc.append(self.color[self.__color_fg1])
self._fill_gc.append(self.color[self._color_bd]) self.__fill_gc.append(self.color[self.__color_bd])
self._fill_hc.append(self.color[self._color_fg]) self.__fill_hc.append(self.color[self.__color_fg])
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_obj.append(self.canvas.create_line( self.__fill_obj.append(self.canvas.create_line(
5, 1, self.w-4, 1, width=1, fill=self.color[self._color_fg1])) 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_fc.append(self.color[self.__color_fg1])
self._fill_gc.append(self.color[self._color_bd]) self.__fill_gc.append(self.color[self.__color_bd])
self._fill_hc.append(self.color[self._color_fg]) self.__fill_hc.append(self.color[self.__color_fg])
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_obj.append(self.canvas.create_line( 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.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_fc.append(self.color[self.__color_fg1])
self._fill_gc.append(self.color[self._color_bd]) self.__fill_gc.append(self.color[self.__color_bd])
self._fill_hc.append(self.color[self._color_fg]) self.__fill_hc.append(self.color[self.__color_fg])
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_obj.append(self.canvas.create_line( 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])) 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_fg3]) self.__fill_fc.append(self.color[self.__color_fg2])
self._fill_gc.append(self.color[self._color_bg1]) self.__fill_gc.append(self.color["background"])
self._fill_hc.append(self.color[self._color_fg2]) self.__fill_hc.append(self.color[self.__color_fg2])
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_obj.append(self.canvas.create_rectangle( self.__fill_obj.append(self.canvas.create_rectangle(
2, 5, self.w-1, self.h-5, width=0, fill=self.color[self._color_fg2])) 2, 5, self.w-1, self.h-5, width=0, fill=self.color[self.__color_fg2]))
self._fill_fc.append(self.color[self._color_fg3]) self.__fill_fc.append(self.color[self.__color_fg2])
self._fill_gc.append(self.color[self._color_bg1]) self.__fill_gc.append(self.color["background"])
self._fill_hc.append(self.color[self._color_fg2]) self.__fill_hc.append(self.color[self.__color_fg2])
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_obj.append(self.canvas.create_rectangle( self.__fill_obj.append(self.canvas.create_rectangle(
5, 2, self.w-4, self.h-2, width=0, fill=self.color[self._color_fg2])) 5, 2, self.w-4, self.h-2, width=0, fill=self.color[self.__color_fg2]))
if (self._color_text is None): self.__fill_fc.append(self.color[self.__color_fg])
self._fill_fc.append(self.color[self._color_fg]) self.__fill_gc.append(self.color["regular_text"])
self._fill_gc.append(self.color["regular_text"]) self.__fill_hc.append(self.color[self.__color_fg])
self._fill_hc.append(self.color[self._color_fg]) self.__fill_func.append(update_color)
else: self.__fill_obj.append(
self._fill_fc.append(self._color_text)
self._fill_gc.append(self._color_text)
self._fill_hc.append(self._color_text)
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))) self.canvas.create_text(int(self.w/2), int(self.h/2), text=self.text, font=(use_font, 10)))
def bind_anim(self): def bind_anim(self):
def anim_magictk(): def anim_magictk():
if (self._is_hover == 1 and self._flash_t < self.max_flash): 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 += (1 if (len(self.root.anim) > 6) else 1)
self._flash_t = min(self._flash_t, self.max_flash) self.__flash_t = min(self.__flash_t, self.max_flash)
self.hover_mode = self._flash_t/self.max_flash self.hover_mode = self.__flash_t/self.max_flash
self._update_color() self.__update_color()
elif (self._is_hover == 0 and self._flash_t > 0): 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 -= (1 if (len(self.root.anim) > 6) else 1)
self._flash_t = max(self._flash_t, 0) self.__flash_t = max(self.__flash_t, 0)
self.hover_mode = self._flash_t/self.max_flash self.hover_mode = self.__flash_t/self.max_flash
self._update_color() self.__update_color()
else: # elif (self.__is_hover == 0 and self.__flash_t <= 0):
return -1 # if self.__anim_obj_id != -1:
# self.__anim_obj_id = None
def anim_normal(*args): def anim_normal(*args):
if (self._is_hover == 1 and self._flash_t < self.max_flash): if (self.__is_hover == 1 and self.__flash_t < self.max_flash):
self._flash_t += 1 self.__flash_t += 1
self.hover_mode = self._flash_t/self.max_flash self.hover_mode = self.__flash_t/self.max_flash
self._update_color() self.__update_color()
elif (self._is_hover == 0 and self._flash_t > 0): elif (self.__is_hover == 0 and self.__flash_t > 0):
self._flash_t -= 1 self.__flash_t -= 1
self.hover_mode = self._flash_t/self.max_flash self.hover_mode = self.__flash_t/self.max_flash
self._update_color() self.__update_color()
self.root.after(anim_normal, 16) self.root.after(anim_normal, 16)
try: try:
@ -218,37 +229,54 @@ class Button:
else: else:
if (anim_magictk not in self.root.anim): if (anim_magictk not in self.root.anim):
self.root.anim.append(anim_magictk) self.root.anim.append(anim_magictk)
self._anim_obj_id = self.root.anim[-1] self.__anim_obj_id = self.root.anim[-1]
def __bind_event(self): def __bind_event(self):
def enter_v(*args): def enter_v(*args):
self.bind_anim() # self.bind_anim()
if (self._is_hover == 0): if (self.__is_hover == 0):
self._is_hover = 1 self.__is_hover = 1
self.canvas.bind("<Enter>", enter_v) self.canvas.bind("<Enter>", enter_v)
def leave_v(*args): def leave_v(*args):
self.bind_anim() # self.bind_anim()
if (self._is_hover == 1): if (self.__is_hover == 1):
self._is_hover = 0 self.__is_hover = 0
self.canvas.bind("<Leave>", leave_v) self.canvas.bind("<Leave>", leave_v)
def press_v(*args): def press_v(*args):
self._is_hover = 2 self.__is_hover = 2
self._update_color() self.__update_color()
self.canvas.bind("<Button-1>", press_v) self.canvas.bind("<Button-1>", press_v)
def pressrelease_v(*args): def pressrelease_v(*args):
self.bind_anim() # self.bind_anim()
self._is_hover = 1 self.__is_hover = 1
self._func(self) self.__func(self)
self._update_color() self.__update_color()
self.canvas.bind("<ButtonRelease-1>", pressrelease_v) self.canvas.bind("<ButtonRelease-1>", pressrelease_v)
class ButtonFill(Button): 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): def __draw_corner(self, r_x, r_y, x, y, **kwargs):
border_info = json.loads(photoload.loadres("buttonborder")) border_info = json.loads(photoload.loadres("buttonborder"))
y_n = 0 y_n = 0
for i in border_info: for i in border_info:
@ -268,50 +296,192 @@ class ButtonFill(Button):
lcolor = j lcolor = j
if (j < 0): if (j < 0):
g_color = color_tmpl.mix_color( g_color = color_tmpl.mix_color(
self.color[self._color_bg], self.color[self._color_bd], int((1-lcolor/255)*1000)/1000) self.color[self.__color_bg], self.color[self.__color_bd], int((1-lcolor/255)*1000)/1000)
else: else:
g_color = color_tmpl.mix_color( g_color = color_tmpl.mix_color(
self.color["background"], self.color[self._color_bd], int((1-lcolor/255)*1000)/1000) self.color["background"], self.color[self.__color_bd], int((1-lcolor/255)*1000)/1000)
if (j < 0): if (j < 0):
f_color = color_tmpl.mix_color( f_color = color_tmpl.mix_color(
self.color[self._color_fg1], self.color[self._color_fg1], int((1-lcolor/255)*1000)/1000) self.color[self.__color_fg1], self.color[self.__color_fg1], int((1-lcolor/255)*1000)/1000)
else: else:
f_color = color_tmpl.mix_color( f_color = color_tmpl.mix_color(
self.color["background"], self.color[self._color_fg1], int((1-lcolor/255)*1000)/1000) self.color["background"], self.color[self.__color_fg1], int((1-lcolor/255)*1000)/1000)
if (j < 0): if (j < 0):
h_color = color_tmpl.mix_color( h_color = color_tmpl.mix_color(
self.color[self._color_fg2], self.color[self._color_fg2], int((1-lcolor/255)*1000)/1000) self.color[self.__color_fg2], self.color[self.__color_fg2], int((1-lcolor/255)*1000)/1000)
else: else:
h_color = color_tmpl.mix_color( h_color = color_tmpl.mix_color(
self.color["background"], self.color[self._color_fg2], int((1-lcolor/255)*1000)/1000) self.color["background"], self.color[self.__color_fg2], int((1-lcolor/255)*1000)/1000)
obj = self.canvas.create_rectangle( obj = self.canvas.create_rectangle(
px, py, px, py, width=0, fill=g_color) px, py, px, py, width=0, fill=g_color)
def update_color(obj, g_color, f_color, h_color): def update_color(obj, g_color, f_color, h_color):
if (self._is_hover == 2): if (self.__is_hover == 2):
self.canvas.itemconfig( self.canvas.itemconfig(
obj, fill=h_color) obj, fill=h_color)
else: else:
self.canvas.itemconfig( self.canvas.itemconfig(
obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode)) obj, fill=color_tmpl.mix_color(g_color, f_color, self.hover_mode))
self._fill_func.append(update_color) self.__fill_func.append(update_color)
self._fill_gc.append(g_color) self.__fill_gc.append(g_color)
self._fill_fc.append(f_color) self.__fill_fc.append(f_color)
self._fill_hc.append(h_color) self.__fill_hc.append(h_color)
self._fill_obj.append(obj) self.__fill_obj.append(obj)
x_n += 1 x_n += 1
y_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", func=lambda s: print("Press"), color_list: None | dict = 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: None | dict = None):
self._color_bd = color_type self.__func = func
self._color_bg = color_type set_font()
self._color_bg1 = color_type self.__color_bd = color_type
self._color_fg = color_type+"_light3" self.__color_bg = color_type
self._color_fg1 = color_type+"_dark" self.__color_fg = color_type
self._color_fg2 = color_type+"_light3" self.__color_fg1 = color_type+"_light3"
self._color_fg3 = color_type+"_dark" self.__color_fg2 = color_type+"_dark"
self._color_text = "#FFFFFF" self.w = max(30, w)
super().__init__(master=master, root_anim=root_anim, w=w, h=h, self.h = max(30, h)
text=text, color_list=color_list, func=func, _set_defaultcolor=True) 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()
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.__func(self)
self.__update_color()
self.canvas.bind("<ButtonRelease-1>", pressrelease_v)

View File

@ -1,269 +0,0 @@
import json
import tkinter
import sys
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
try:
import submenu
except ImportError:
from magictk import submenu
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 Entry:
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 = 6
__anim_obj_id = -1
__color_bd = "border_base"
__color_bg = "background"
__color_fg = "primary"
__color_fg1 = "primary"
__color_fg2 = "background"
text = "Input"
input_text = ""
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=200, h=30, text="Input", color_list: None | dict = None, items=[]):
set_font()
self.input_text = ""
self.items = items
self.w = max(120, w)
self.h = max(30, h)
self.text = text
self.choose = -1
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()
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.__text_obj = self.canvas.create_text(
20, int(self.h/2), text=self.text, font=(use_font, 10), justify="left")
self.__fill_fc.append(self.color["placeholder"])
self.__fill_gc.append(self.color["placeholder"])
self.__fill_hc.append(self.color["placeholder"])
self.__fill_func.append(update_color)
self.__fill_obj.append(self.__text_obj)
self.canvas.moveto(self.__text_obj, 16, self.h//2-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 __callback_menu(self, obj, ids):
self.__menuobj.menu_effect[self.__last] = {}
self.__menuobj.menu_effect[ids] = {
"fill": self.color["primary"], "font": (obj.font[0], obj.font[1], "bold")}
self.__last = ids
self.canvas.itemconfigure(self.__text_obj, text=self.items[ids])
self.canvas.moveto(self.__text_obj, 16, self.h//2-10)
def __bind_event(self):
def closecallback(obj):
self.__is_hover = 0
def pressrelease_v(event: tkinter.Event):
if (self.__is_hover == 1):
self.__is_hover = 0
else:
self.__is_hover = 1
self.top = tkinter.Toplevel()
self.top.geometry(
f"{self.w}x{self.h}+{event.x_root-event.x}+{event.y_root-event.y}")
self.top.wm_attributes("-alpha", 0.5)
self.__top_entry = ttk.Entry(self.top, width=self.w)
self.__top_entry.place(x=0, y=0, width=self.w, height=self.h)
self.__top_entry.focus_force()
self.__update_color()
self.canvas.bind("<ButtonRelease-1>", pressrelease_v)

View File

@ -1,26 +0,0 @@
from tkinter import font as tkfont
font = None
def set_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")
return use_font
def getfont():
global font
if (font is None):
font = set_font()
return font