更新button和window

This commit is contained in:
cxykevin 2024-04-07 21:38:07 +08:00
parent 913c4a3496
commit c60307aa4c
2 changed files with 29 additions and 23 deletions

View File

@ -108,16 +108,16 @@ class Button:
def __init__(self, master=None, root_anim=None, w=80, h=30, text="Button", func=lambda s: print("Press"), color_list: None | dict = 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):
set_font() set_font()
self.__func = func self.__func = func
self.w = max(80, w) self.w = max(30, w)
self.h = max(30, h) self.h = max(30, h)
self.text = text self.text = text
self.__master = master self.__master = master
if (color_list is not None): if (color_list is not None):
self.color = color_list self.color = color_list
if (root_anim == None): if (root_anim == None):
self.__root = master self.root = master.root
else: else:
self.__root = root_anim self.root = root_anim
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)
@ -198,12 +198,12 @@ class Button:
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()
@ -220,16 +220,16 @@ class Button:
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:
self.__root.anim == 0 self.root.anim == 0
except: except:
self.__root.after(anim_normal, 16) self.root.after(anim_normal, 16)
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):
@ -347,16 +347,16 @@ class ButtonFill(Button):
self.__color_fg = color_type self.__color_fg = color_type
self.__color_fg1 = color_type+"_light3" self.__color_fg1 = color_type+"_light3"
self.__color_fg2 = color_type+"_dark" self.__color_fg2 = color_type+"_dark"
self.w = max(80, w) self.w = max(30, w)
self.h = max(30, h) self.h = max(30, h)
self.text = text self.text = text
self.__master = master self.__master = master
if (color_list is not None): if (color_list is not None):
self.color = color_list self.color = color_list
if (root_anim == None): if (root_anim == None):
self.__root = master self.root = master.root
else: else:
self.__root = root_anim self.root = root_anim
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)
@ -428,12 +428,12 @@ class ButtonFill(Button):
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()
@ -450,16 +450,16 @@ class ButtonFill(Button):
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:
self.__root.anim == 0 self.root.anim == 0
except: except:
self.__root.after(anim_normal, 16) self.root.after(anim_normal, 16)
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):

View File

@ -22,6 +22,7 @@ try:
import photoload import photoload
except ImportError: except ImportError:
from magictk import photoload from magictk import photoload
import sys
WIN_INF = -10000 WIN_INF = -10000
@ -152,6 +153,7 @@ class Window(ttk.Frame):
titles.bind("<ButtonRelease-1>", special_move) titles.bind("<ButtonRelease-1>", special_move)
def __init__(self, w=500, h=350, title="MagicTk", color_list: None | dict = None) -> None: def __init__(self, w=500, h=350, title="MagicTk", color_list: None | dict = None) -> None:
self.root = self
self.title = title self.title = title
self.w = w self.w = w
self.h = h self.h = h
@ -196,9 +198,13 @@ class Window(ttk.Frame):
delta_t = time.time()-t_start delta_t = time.time()-t_start
if (delta_t > 0.02): # flash animation if (delta_t > 0.02): # flash animation
t_start = time.time() t_start = time.time()
n = 0
for i in self.anim: for i in self.anim:
if (i is not None): if (i is not None):
i() retn = i()
if (retn == -1):
self.anim[n] = None
n += 1
self.anim = [i for i in self.anim if i is not None] self.anim = [i for i in self.anim if i is not None]
else: else:
pass pass