完成entry

This commit is contained in:
cxykevin 2024-04-14 22:11:17 +08:00
parent 08669cdd1b
commit d084d3427f
1 changed files with 74 additions and 54 deletions

View File

@ -2,34 +2,10 @@ import json
import tkinter import tkinter
import sys import sys
from tkinter import ttk from tkinter import ttk
from tkinter import font as tkfont from magictk import color_tmpl
try: from magictk import photoload
import color_tmpl from magictk import fontconfig
except ImportError: from magictk import button
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: class Entry:
@ -112,7 +88,8 @@ class Entry:
n += 1 n += 1
def __init__(self, master=None, root_anim=None, w=200, h=30, text="Input", color_list: dict = None, items=[]): def __init__(self, master=None, root_anim=None, w=200, h=30, text="Input", color_list: dict = None, items=[]):
set_font() global use_font
use_font = fontconfig.getfont()
self.input_text = "" self.input_text = ""
self.items = items self.items = items
self.w = max(120, w) self.w = max(120, w)
@ -127,8 +104,39 @@ class Entry:
else: else:
self.root = root_anim self.root = root_anim
self.frames = tkinter.Frame(
master, width=self.w, height=self.h, bg=self.color["background"])
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) self.frames,
bg=self.color["background"],
width=self.w,
height=self.h,
borderwidth=0,
bd=0,
highlightcolor=self.color["background"],
highlightthickness=0
)
self.canvas.place(x=0, y=0, relwidth=1.0, relheight=1.0)
self.__input_textvar = tkinter.StringVar()
self.inputobj = tkinter.Entry(
self.frames,
bg=self.color["background"],
fg=self.color["regular_text"],
highlightthickness=0,
bd=0,
font=(use_font, 10),
textvariable=self.__input_textvar
)
self.inputobj.place(x=10, y=6, width=self.w-20, height=self.h-12)
self.show_label = tkinter.Label(self.frames,
text=self.text,
bg=self.color["background"],
fg=self.color["secondary_text"],
anchor="w",
font=(use_font, 10)
)
self.show_label.place(x=10, y=6, width=self.w-20, height=self.h-12)
self.packed = True
self.__draw() self.__draw()
self.__update_color() self.__update_color()
@ -136,13 +144,13 @@ class Entry:
self.bind_anim() self.bind_anim()
def pack(self, *args, **kwargs): def pack(self, *args, **kwargs):
self.canvas.pack(*args, **kwargs) self.frames.pack(*args, **kwargs)
def guid(self, *args, **kwargs): def guid(self, *args, **kwargs):
self.canvas.guid(*args, **kwargs) self.frames.guid(*args, **kwargs)
def place(self, *args, **kwargs): def place(self, *args, **kwargs):
self.canvas.place(*args, **kwargs) self.frames.place(*args, **kwargs)
def __draw(self): def __draw(self):
self.__draw_corner(0, 0, 0, 0) self.__draw_corner(0, 0, 0, 0)
@ -217,9 +225,6 @@ class Entry:
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()
# 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): 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):
@ -240,30 +245,45 @@ class Entry:
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 __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 __bind_event(self):
def closecallback(obj): def closecallback(obj):
if (self.__is_hover == 1):
self.top.destroy()
self.__is_hover = 0 self.__is_hover = 0
self.canvas.focus_force()
def pressrelease_v(event: tkinter.Event): def pressrelease_v(event: tkinter.Event):
if (self.__is_hover == 1): if (self.__is_hover == 1):
self.__is_hover = 0 closecallback(event)
else: else:
self.__is_hover = 1 self.__is_hover = 1
if sys.platform.startswith("linux"): # what's fuck?
self.top = tkinter.Toplevel() self.top = tkinter.Toplevel()
self.top.geometry( self.top.geometry(
f"{self.w}x{self.h}+{event.x_root-event.x}+{event.y_root-event.y}") f"{1}x{1}+{10000}+{10000}")
self.top.wm_attributes("-alpha", 0.5)
self.__top_entry = ttk.Entry(self.top, width=self.w) 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.place(
self.__top_entry.focus_force() x=0, y=0, width=self.w, height=self.h)
self.root.update()
self.inputobj.focus_force()
self.__update_color() self.__update_color()
def update_text(*args):
self.input_text = self.__input_textvar.get()
if (self.input_text == ""):
if (self.packed == False):
self.show_label.place(
x=10, y=6, width=self.w-20, height=self.h-12)
self.packed = True
else:
if (self.packed):
self.show_label.place_forget()
self.packed = False
self.frames.bind("<ButtonRelease-1>", pressrelease_v)
self.canvas.bind("<ButtonRelease-1>", pressrelease_v) self.canvas.bind("<ButtonRelease-1>", pressrelease_v)
self.inputobj.bind("<ButtonRelease-1>", pressrelease_v)
self.show_label.bind("<ButtonRelease-1>", pressrelease_v)
self.__input_textvar.trace_add("write", update_text)
# self.frames.bind_all("<FocusOut>", closecallback)
if (sys.platform.startswith("linux")):
self.frames.bind("<Leave>", closecallback)