初步完成scrollframe,缺失自动配置大小等
Build and Publish / Run (push) Successful in 44s Details

This commit is contained in:
cxykevin 2024-04-21 22:22:06 +08:00
parent 983dff8921
commit 1de2bda297
4 changed files with 49 additions and 54 deletions

View File

@ -85,8 +85,20 @@ if __name__ == "__main__":
magictk.Entry(frame7, w=200).pack(side='left')
frame7.pack()
frame8 = magictk.Frame(win)
magictk.ScrollBar(frame8, h=200, allh=100).pack(side='left')
frame8 = magictk.Container(win, h=10, container_h=30*6*5)
for i in range(5):
magictk.Button(frame8, text="Default",
func=lambda s: print("Btn 1")).pack()
magictk.ButtonFill(frame8, text="Primary",
func=lambda s: print("Btn 2")).pack()
magictk.ButtonFill(frame8, color_type="success", text="Success",
func=lambda s: print("Btn 3")).pack()
magictk.ButtonFill(frame8, color_type="info", text="Info",
func=lambda s: print("Btn 4")).pack()
magictk.ButtonFill(frame8, color_type="warning", text="Warning",
func=lambda s: print("Btn 5")).pack()
magictk.ButtonFill(frame8, color_type="danger", text="Danger",
func=lambda s: print("Btn 6")).pack()
frame8.pack()
win.mainloop()

View File

@ -5,6 +5,6 @@ from magictk.progressbar import ProgressBar
from magictk.checkbox import Checkbox, RadioGroup
from magictk.submenu import Menu, MenuObjs
from magictk.select import Select
from magictk.frame import Frame
from magictk.frame import Frame, Container
from magictk.entry import Entry
from magictk.scrollbar import ScrollBar

View File

@ -1,6 +1,7 @@
import tkinter
from tkinter import ttk
from magictk import color_tmpl
from magictk.scrollbar import ScrollBar
class Frame(tkinter.Frame):
@ -8,6 +9,35 @@ class Frame(tkinter.Frame):
def __init__(self, master, color=None, *args, **kwargs):
self.root = master.root
kwargs["bg"] = self.color["background"]
if ("bg" not in kwargs and "background" not in kwargs):
kwargs["bg"] = self.color["background"]
super().__init__(master, *args, **kwargs)
self.configure()
class Container(Frame):
color = color_tmpl.default_color
def scroll_callback(self, obj, pos):
super().place(x=0, y=-pos, w=self.w-7, h=self.container_h)
def __init__(self, master, color=None, w=300, h=200, container_h=500, *args, **kwargs):
self.root = master.root
self.w = w
self.h = h
self.container_h = container_h
self.root_frame = Frame(master, width=w, height=h)
super().__init__(self.root_frame, *args, **kwargs)
super().place(x=0, y=0, w=self.w-7, h=container_h)
self.scroll = ScrollBar(self.root_frame,
h=self.h, allh=self.container_h-self.h, maxh=self.h, callback=self.scroll_callback)
self.scroll.place(x=self.w-7, y=0, w=8, relheight=1)
def pack(self, *args, **kwargs):
self.root_frame.pack(*args, **kwargs)
def grid(self, *args, **kwargs):
self.root_frame.grid(*args, **kwargs)
def place(self, *args, **kwargs):
self.root_frame.place(*args, **kwargs)

View File

@ -59,9 +59,10 @@ class ScrollBar:
y_n += 1
def __init__(self, master=None, root_anim=None, w=None, h=8, colors="primary", color_list: dict = None, allh=100,
maxh=50):
maxh=50, callback=lambda obj, val: None):
self._r_allh = allh
self._r_maxh = maxh
self.callback = callback
self.w = 8
self.h = h
self.__master = master
@ -125,7 +126,7 @@ class ScrollBar:
delta_y = event.y-self.scroll_y
self.scroll_y = min(self.h-self.bar_len,
max(0, self.scroll_y+delta_y-self.move_start))
print(self.get())
self.callback(self, self.get())
self.update_pos()
def mouse_release(event: tkinter.Event):
@ -152,54 +153,6 @@ class ScrollBar:
self.__draw_corner(0, 1, 0, self.scroll_y+self.bar_len-4+1,
self.color["border_light"], "bottomside", bgcolor=self.color["background"])
def __update_pixel(self):
self.__move_progress_pixel = max(4, int((self.w-6)*self.progress))
def add_progress(self, n):
self.progress = max(min(self.progress+n, 1.0), 0.0)
self.__update_pixel()
def set_progress(self, n):
self.progress = max(min(n, 1.0), 0.0)
self.__update_pixel()
def bind_anim(self):
def anim_magictk():
if (int(self.__progress_pixel) < int(self.__move_progress_pixel)):
if (self.__move_progress_pixel-self.__progress_pixel > 8):
self.__progress_pixel += 8
else:
add_n = int(
(self.__progress_pixel-self.__move_progress_pixel)/2)
if (add_n <= 2):
self.__progress_pixel = self.__move_progress_pixel
else:
self.__progress_pixel += add_n
self.__update_prog()
elif (int(self.__progress_pixel) > int(self.__move_progress_pixel)):
if (self.__progress_pixel-self.__move_progress_pixel > 8):
self.__progress_pixel -= 8
else:
add_n = int(
(self.__progress_pixel-self.__move_progress_pixel)/2)
if (add_n <= 2):
self.__progress_pixel = self.__move_progress_pixel
else:
self.__progress_pixel -= add_n
self.__update_prog()
def anim_normal(*args):
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 get(self):
if (self.scroll_y+self.bar_len >= self.h):
return self._r_allh