完成scrollframe,嵌套事件暂时存在问题
Build and Publish / Run (push) Successful in 51s Details

This commit is contained in:
cxykevin 2024-04-24 22:18:09 +08:00
parent c860771118
commit 546172ee07
9 changed files with 61 additions and 15 deletions

View File

@ -2,7 +2,8 @@ import magictk
import random import random
from multiprocessing import Process from multiprocessing import Process
if __name__ == "__main__": if __name__ == "__main__":
win = magictk.Window() mwin = magictk.Window()
win = magictk.Container(mwin, h=10000, container_h=500)
frame = magictk.Frame(win) frame = magictk.Frame(win)
magictk.Button(frame, text="Default", magictk.Button(frame, text="Default",
@ -28,7 +29,7 @@ if __name__ == "__main__":
for i in range(1, 10): for i in range(1, 10):
obj1.addmenu(f"Option {i}", lambda s, t: print(i)) obj1.addmenu(f"Option {i}", lambda s, t: print(i))
magictk.Button(frame2, text="Menu", magictk.Button(frame2, text="Menu",
func=lambda s: magictk.Menu(win, menuobj=obj1), w=60).pack() func=lambda s: magictk.Menu(mwin, menuobj=obj1), w=60).pack()
frame2.pack() frame2.pack()
frame3 = magictk.Frame(win) frame3 = magictk.Frame(win)
@ -85,8 +86,8 @@ if __name__ == "__main__":
magictk.Entry(frame7, w=200).pack(side='left') magictk.Entry(frame7, w=200).pack(side='left')
frame7.pack() frame7.pack()
frame8 = magictk.Container(win, h=100, container_h=30*6*2) frame8 = magictk.Container(win, h=200, container_h=30*6*5)
for i in range(2): for i in range(5):
magictk.Button(frame8, text="Default", magictk.Button(frame8, text="Default",
func=lambda s: print("Btn 1")).pack() func=lambda s: print("Btn 1")).pack()
magictk.ButtonFill(frame8, text="Primary", magictk.ButtonFill(frame8, text="Primary",
@ -99,6 +100,8 @@ if __name__ == "__main__":
func=lambda s: print("Btn 5")).pack() func=lambda s: print("Btn 5")).pack()
magictk.ButtonFill(frame8, color_type="danger", text="Danger", magictk.ButtonFill(frame8, color_type="danger", text="Danger",
func=lambda s: print("Btn 6")).pack() func=lambda s: print("Btn 6")).pack()
frame8.pack(expand="y") frame8.pack(expand=True, fill="x")
win.mainloop() win.pack(expand=True, fill="x", anchor="n")
mwin.mainloop()

View File

@ -56,6 +56,7 @@ class Tk(ttk.Frame):
self.title = title self.title = title
self.w = w self.w = w
self.h = h self.h = h
self.p_event_list = ()
if (color_list is not None): if (color_list is not None):
self.color = color_list self.color = color_list
self.main_tk = tkinter.Tk() self.main_tk = tkinter.Tk()

View File

@ -112,6 +112,9 @@ class Button:
self._bind_event() self._bind_event()
self.bind_anim() self.bind_anim()
for event, eventfunc in master.p_event_list:
self.canvas.bind(event, eventfunc)
def pack(self, *args, **kwargs): def pack(self, *args, **kwargs):
self.canvas.pack(*args, **kwargs) self.canvas.pack(*args, **kwargs)

View File

@ -132,6 +132,9 @@ class Checkbox(button.ButtonFill):
if (group is not None): if (group is not None):
group._add_checkbox(self) group._add_checkbox(self)
for event, eventfunc in master.p_event_list:
self.canvas.bind(event, eventfunc)
def pack(self, *args, **kwargs): def pack(self, *args, **kwargs):
self.canvas.pack(*args, **kwargs) self.canvas.pack(*args, **kwargs)

View File

@ -142,6 +142,10 @@ class Entry:
self.__update_color() self.__update_color()
self.__bind_event() self.__bind_event()
self.bind_anim() self.bind_anim()
for event, eventfunc in master.p_event_list:
self.canvas.bind(event, eventfunc)
self.show_label.bind(event, eventfunc)
self.inputobj.bind(event, eventfunc)
def pack(self, *args, **kwargs): def pack(self, *args, **kwargs):
self.frames.pack(*args, **kwargs) self.frames.pack(*args, **kwargs)

View File

@ -2,6 +2,7 @@ import tkinter
from tkinter import ttk from tkinter import ttk
from magictk import color_tmpl from magictk import color_tmpl
from magictk.scrollbar import ScrollBar from magictk.scrollbar import ScrollBar
import sys
class Frame(tkinter.Frame): class Frame(tkinter.Frame):
@ -9,11 +10,15 @@ class Frame(tkinter.Frame):
def __init__(self, master, color=None, *args, **kwargs): def __init__(self, master, color=None, *args, **kwargs):
self.root = master.root self.root = master.root
self.p_event_list = ()
if ("bg" not in kwargs and "background" not in kwargs): if ("bg" not in kwargs and "background" not in kwargs):
kwargs["bg"] = self.color["background"] kwargs["bg"] = self.color["background"]
super().__init__(master, *args, **kwargs) super().__init__(master, *args, **kwargs)
self.configure() self.configure()
for event, eventfunc in master.p_event_list:
self.bind(event, eventfunc)
class Container(Frame): class Container(Frame):
color = color_tmpl.default_color color = color_tmpl.default_color
@ -24,12 +29,6 @@ class Container(Frame):
def update_frame(self, *args, **kwargs): def update_frame(self, *args, **kwargs):
self.w = self.root_frame.winfo_width() self.w = self.root_frame.winfo_width()
self.h = self.root_frame.winfo_height() self.h = self.root_frame.winfo_height()
# if (self.h >= self.container_h-2):
# # self.scroll.scroll_y = 0
# # self.scroll.update_pos()
# # self.scroll.place_forget()
# pass
# else:
super().place(x=0, y=-self.scroll.get(), w=self.w-7, h=self.container_h) super().place(x=0, y=-self.scroll.get(), w=self.w-7, h=self.container_h)
self.scroll._r_allh = self.container_h self.scroll._r_allh = self.container_h
self.scroll._r_maxh = self.h self.scroll._r_maxh = self.h
@ -38,11 +37,24 @@ class Container(Frame):
scrolltmp = self.scroll.scroll_y*self.scroll.y_size scrolltmp = self.scroll.scroll_y*self.scroll.y_size
self.scroll.cal_bar() self.scroll.cal_bar()
self.scroll.update_pos() self.scroll.update_pos()
if (self.scroll.y_size==0): if (self.scroll.y_size == 0):
self.scroll.scroll_y=0 self.scroll.scroll_y = 0
else: else:
self.scroll.scroll_y = min(self.scroll.h-self.scroll.bar_len, self.scroll.scroll_y = min(self.scroll.h-self.scroll.bar_len,
max(0, scrolltmp/self.scroll.y_size)) max(0, scrolltmp/self.scroll.y_size))
self.scroll_callback(self.scroll, self.scroll.get())
def scrollwheel(self, event: tkinter.Event, setdelta=None):
if (setdelta is None):
delta = event.delta
else:
delta = setdelta
if sys.platform.startswith('win'):
delta //= 20
self.scroll.scroll_y = max(
0, min(self.scroll._r_maxh-self.scroll.bar_len, self.scroll.scroll_y+delta))
self.scroll.update_pos()
self.scroll_callback(self.scroll, self.scroll.get())
def __init__(self, master, color=None, w=300, h=200, container_h=500, *args, **kwargs): def __init__(self, master, color=None, w=300, h=200, container_h=500, *args, **kwargs):
self.root = master.root self.root = master.root
@ -56,6 +68,20 @@ class Container(Frame):
h=self.h, allh=self.container_h, maxh=self.h, callback=self.scroll_callback) h=self.h, allh=self.container_h, maxh=self.h, callback=self.scroll_callback)
self.scroll.place(x=self.w-7, y=0, w=8, relheight=1) self.scroll.place(x=self.w-7, y=0, w=8, relheight=1)
self.root_frame.bind("<Configure>", self.update_frame) self.root_frame.bind("<Configure>", self.update_frame)
if sys.platform.startswith('win'):
self.bind("<MouseWheel>", self.scrollwheel)
self.p_event_list = (("<MouseWheel>", self.scrollwheel),)
elif sys.platform.startswith('darwin'):
self.bind("<MouseWheel>", self.scrollwheel)
self.p_event_list = (("<MouseWheel>", self.scrollwheel),)
else:
self.bind(
"<Button-4>", lambda event: self.scrollwheel(event, -10))
self.bind(
"<Button-5>", lambda event: self.scrollwheel(event, 10))
self.p_event_list = (
("<Button-4>", lambda event: self.scrollwheel(event, -10)), ("<Button-5>", lambda event: self.scrollwheel(event, 10)))
# self.configure # self.configure
def pack(self, *args, **kwargs): def pack(self, *args, **kwargs):

View File

@ -69,6 +69,8 @@ class ProgressBar:
self.__draw() self.__draw()
# self.__bind_event() # self.__bind_event()
self.bind_anim() self.bind_anim()
for event, eventfunc in master.p_event_list:
self.canvas.bind(event, eventfunc)
def pack(self, *args, **kwargs): def pack(self, *args, **kwargs):
self.canvas.pack(*args, **kwargs) self.canvas.pack(*args, **kwargs)

View File

@ -65,6 +65,9 @@ class Select(button.Button):
for i in self.items: for i in self.items:
self.__menuobj.addmenu(i, self._callback_menu) self.__menuobj.addmenu(i, self._callback_menu)
for event, eventfunc in master.p_event_list:
self.canvas.bind(event, eventfunc)
def _draw(self): def _draw(self):
super()._draw(True) super()._draw(True)

View File

@ -143,6 +143,7 @@ class Window(ttk.Frame):
self.title = title self.title = title
self.w = w self.w = w
self.h = h self.h = h
self.p_event_list = ()
if (color_list is not None): if (color_list is not None):
self.color = color_list self.color = color_list
self.main_tk = tkinter.Tk() self.main_tk = tkinter.Tk()