tkinter-yzw 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ from .tk_mainui import *
2
+ from .tk_autoscrollbar import *
3
+ from .tk_autoscrollbar import *
4
+ from .tk_dialog import *
5
+ from .tk_listedit import *
6
+ from .tk_listview import *
7
+ from .tk_index import *
8
+ from .tk_pages import *
9
+ from .tk_sheet import *
10
+ from .tk_table import *
11
+ from .tk_text import *
12
+ from .tk_tooltip import *
13
+ from .tk_tree import *
@@ -0,0 +1,155 @@
1
+
2
+
3
+ # http://effbot.org/zone/tkinter-autoscrollbar.htm
4
+ # http://effbot.org/zone/tkinter-scrollbar-patterns.htm
5
+
6
+ import tkinter as tk
7
+
8
+
9
+ class _AutoScrollbar_pack(tk.Scrollbar):
10
+ def set(self, lo, hi):
11
+ # print("_AutoScrollbar set(%r,%r)"%(lo, hi))
12
+ if float(lo) <= 0.0 and float(hi) >= 1.0:
13
+ self.pack_forget()
14
+ else:
15
+ if self.cget("orient") == tk.HORIZONTAL:
16
+ self.pack(fill=tk.X, side=tk.BOTTOM)
17
+ else:
18
+ self.pack(fill=tk.Y, side=tk.RIGHT)
19
+ tk.Scrollbar.set(self, lo, hi)
20
+
21
+
22
+ class _AutoScrollbar(tk.Scrollbar):
23
+ def set(self, lo, hi):
24
+ # print("_AutoScrollbar set(%r,%r)" % (lo, hi))
25
+ if float(lo) <= 0.0 and float(hi) >= 1.0:
26
+ self.grid_remove()
27
+ else:
28
+ self.grid()
29
+ tk.Scrollbar.set(self, lo, hi)
30
+
31
+
32
+ class TkYzwFrameAutoScroll(tk.Frame):
33
+ """"
34
+ 给不支持滚动的控件使用,即Text,Canvas,Listbox,Entry之外的控件
35
+ """
36
+ def __init__(self, master=None, scroll="xy", **kw):
37
+ """
38
+ :param master: canvas's parent
39
+ """
40
+
41
+ master.columnconfigure(0, weight=1)
42
+ master.rowconfigure(0, weight=1)
43
+
44
+ scrollcommand = dict()
45
+ xbar, ybar = None, None
46
+ if 'x' in scroll:
47
+ self.ui_xbar = xbar = _AutoScrollbar(master, orient=tk.HORIZONTAL)
48
+ xbar.grid(row=1, column=0, sticky="ew")
49
+ scrollcommand["xscrollcommand"] = xbar.set
50
+ if 'y' in scroll:
51
+ self.ui_ybar = ybar = _AutoScrollbar(master)
52
+ ybar.grid(row=0, column=1, sticky="ns")
53
+ scrollcommand["yscrollcommand"] = ybar.set
54
+
55
+ self.ui_canvas = canvas = tk.Canvas(master, **scrollcommand)
56
+ canvas.grid(row=0, column=0, sticky="nsew")
57
+
58
+ if xbar: xbar.config(command=canvas.xview)
59
+ if ybar: ybar.config(command=canvas.yview)
60
+
61
+ super().__init__(canvas, **kw)
62
+
63
+ def update_scrollregion(self):
64
+ """
65
+ should be call when self packed
66
+ """
67
+ self.ui_canvas.create_window(0, 0, anchor=tk.NW, window=self) # 相当于self.pack()
68
+ self.update_idletasks() # to update bbox
69
+ self.ui_canvas.config(scrollregion=self.ui_canvas.bbox("all"))
70
+
71
+
72
+ def enable_autoscroll(cls, master_, scroll="xy", *a, **ka):
73
+ """
74
+ cls: Text,Canvas,Listbox,Entry
75
+ """
76
+ master = tk.Frame(master_, bd=2, relief="sunken")
77
+ master.columnconfigure(0, weight=1)
78
+ master.rowconfigure(0, weight=1)
79
+
80
+ xbar, ybar = None, None
81
+ if 'x' in scroll:
82
+ xbar = _AutoScrollbar(master, orient=tk.HORIZONTAL)
83
+ xbar.grid(row=1, column=0, sticky="ew")
84
+ ka["xscrollcommand"] = xbar.set
85
+ if 'y' in scroll:
86
+ ybar = _AutoScrollbar(master)
87
+ ybar.grid(row=0, column=1, sticky="ns")
88
+ ka["yscrollcommand"] = ybar.set
89
+
90
+ obj = cls(master, **ka)
91
+ obj.grid(row=0, column=0, sticky="nsew")
92
+
93
+ if xbar: xbar.config(command=obj.xview)
94
+ if ybar: ybar.config(command=obj.yview)
95
+ obj.pack = master.pack
96
+ obj.grid = master.grid
97
+ obj.place = master.place
98
+ obj.pack_forget = master.pack_forget
99
+ obj.grid_forget = master.grid_forget
100
+ obj.place_forget = master.place_forget()
101
+ return obj
102
+
103
+
104
+ def at_enable_autoscroll(xy="xy"):
105
+ def modified_cls(cls):
106
+ def cls_init(master, *a, **ka):
107
+ return enable_autoscroll(cls, master, scroll=xy, *a, **ka)
108
+ return cls_init
109
+ return modified_cls
110
+
111
+
112
+ # xvar only when wrap="none"
113
+ @at_enable_autoscroll()
114
+ class TextAutoScroll(tk.Text): pass
115
+
116
+
117
+ @at_enable_autoscroll()
118
+ class CanvasAutoScroll(tk.Canvas): pass
119
+
120
+
121
+ @at_enable_autoscroll()
122
+ class ListboxAutoScroll(tk.Listbox): pass
123
+
124
+
125
+ @at_enable_autoscroll()
126
+ class EntryAutoScroll(tk.Entry): pass
127
+
128
+
129
+ if __name__ == '__main__':
130
+ def debugme_1():
131
+ root = tk.Tk()
132
+ frame = TkYzwFrameAutoScroll(root, bg="red")
133
+ label = tk.Label(frame, text="text", font=("Arial", "512"))
134
+ label.pack(fill="both", expand=1)
135
+
136
+ frame.update_scrollregion()
137
+
138
+ root.mainloop()
139
+
140
+
141
+ def debugme_2():
142
+ root = tk.Tk()
143
+ text = enable_autoscroll(tk.Text, root, wrap="none") # type: tk.Text
144
+ text.pack(fill="both", expand=1)
145
+ root.mainloop()
146
+
147
+
148
+ def debugme_3():
149
+ root = tk.Tk()
150
+ text = TextAutoScroll(root, wrap="none")
151
+ text.pack(fill="both", expand=1)
152
+ root.mainloop()
153
+
154
+
155
+ debugme_3()
@@ -0,0 +1,115 @@
1
+ # coding: gbk
2
+ import tkinter as tk
3
+
4
+
5
+ class TkYzwDialog(tk.Toplevel):
6
+ def __init__(self, master_, title=None, modal=True, transient=True, *la, **ka):
7
+ super().__init__(master_, *la, **ka)
8
+ self.master_ = master_
9
+ if title is not None:
10
+ self.title(title)
11
+
12
+ master = self.master # ���master_�ǿ�, tk���Զ�����һ��root��self.master
13
+ if master_ is None and modal:
14
+ master.withdraw()
15
+ transient = False
16
+ if transient: self.transient(master)
17
+ self.modal = modal
18
+ if modal: self.grab_set()
19
+
20
+ #self.geometry("400x300")
21
+ x,y = master.winfo_pointerxy()
22
+ # self.geometry("+%d+%d"%(x-200,y-100))
23
+ self.geometry("+%d+%d" % (x, y))
24
+
25
+ self.result = None
26
+
27
+ def close(self, result):
28
+ self.result = result
29
+ self.destroy()
30
+
31
+ def on_ok(self):
32
+ self.destroy()
33
+ self.result = self.collect_uiv()
34
+
35
+ def on_cancel(self):
36
+ self.destroy()
37
+ self.result = None
38
+
39
+ def collect_uiv(self):
40
+ d = dict()
41
+ for uiv in self.__dict__:
42
+ if uiv.startswith("uiv_"):
43
+ k = uiv[4:]
44
+ wx = getattr(self, uiv)
45
+ d[k] = wx.get()
46
+ return d
47
+
48
+ def run(self):
49
+ # only necessary for modal
50
+ if self.modal:
51
+ self.master.wait_window(self)
52
+ if self.master_ is None:
53
+ self.master.destroy()
54
+ return self.result
55
+ else:
56
+ return None
57
+
58
+
59
+ if __name__ == '__main__':
60
+
61
+ def main_��������modal():
62
+
63
+ class DlgDemo(TkYzwDialog):
64
+ def __init__(self, master, *la, **ka):
65
+ super().__init__(master, *la, **ka)
66
+
67
+ self.uiv_entry = tk.IntVar(value=1)
68
+ tk.Entry(self, width=4, textvariable=self.uiv_entry, justify="center").pack(fill="both", expand=0)
69
+
70
+ fr = tk.Frame(self)
71
+ fr.pack(side="top", pady=5, fill="both")
72
+ tk.Button(fr, text="ȷ��", command=self.on_ok).pack(side="left", fill="both", expand=1)
73
+ tk.Button(fr, text="ȡ��", command=self.on_cancel).pack(side="left", fill="both", expand=1)
74
+
75
+ dlg = DlgDemo(None, modal=True)
76
+ result = dlg.run()
77
+ print(result)
78
+
79
+ dlg = DlgDemo(None, modal=True)
80
+ result = dlg.run()
81
+ print(result)
82
+
83
+ dlg = DlgDemo(None, modal=True)
84
+ result = dlg.run()
85
+ print(result)
86
+
87
+ def main_listview():
88
+ from tk_listview import TkYzwFrameListview
89
+ class DlgDemo(TkYzwDialog):
90
+ def __init__(self, master, a_col, a_row):
91
+ super().__init__(master, modal=True)
92
+
93
+ # a_col = [("��Ʊ", 100), ("�۸�", 100, int), ("����", "50:100,w")]
94
+ lv = TkYzwFrameListview(self, a_col, scroll="y")
95
+ lv.pack(side="top", fill="x")
96
+ for x in a_row: lv.insert(x, index='end')
97
+
98
+ fr = tk.Frame(self)
99
+ fr.pack(side="top", pady=5, fill="both")
100
+ tk.Button(fr, text="ȷ��", command=self.on_ok).pack(side="left", fill="both", expand=1)
101
+ tk.Button(fr, text="ȡ��", command=self.on_cancel).pack(side="left", fill="both", expand=1)
102
+
103
+ a_col = [("��Ʊ", 100), ("�۸�", 100, float), ("����", "50:100,w", int)]
104
+ a_row = [
105
+ ("dsfsd", 2.3, 100),
106
+ ("dsfsd2", 2.1, 1200),
107
+ ("dsfsd3", 2.9, 130),
108
+ ("dsfsd4", 2.4, 1400),
109
+ ]
110
+ dlg = DlgDemo(None, a_col, a_row)
111
+ result = dlg.run()
112
+ print(result)
113
+
114
+
115
+ main_listview()
@@ -0,0 +1,118 @@
1
+
2
+ import tkinter as tk
3
+ from collections import deque
4
+
5
+
6
+ class TkYzwFrameIndexedListbox(tk.Frame):
7
+ def __init__(self, master, dict_index:dict, index_home=['help', 'todo'], command=None, *a, **ka):
8
+ super().__init__(master, *a, **ka)
9
+ self.cb_command = command
10
+ self.dict_index = dict_index
11
+ self.index_home = index_home
12
+ self.history = deque(maxlen=16)
13
+
14
+ fr = tk.Frame(self); fr.pack(side="top", fill="x")
15
+ self.ui_label = tk.Label(fr, text="key:")
16
+ self.ui_label.bind("<Double-Button-1>", self.on_index_double_click)
17
+ self.ui_label.bind("<Button-1>", self.on_label_click)
18
+ self.ui_label.pack(side="left")
19
+
20
+ self.uiv_entry = tk.StringVar()
21
+ self.ui_entry = tk.Entry(fr, width=4, textvariable=self.uiv_entry)
22
+ self.ui_entry.bind("<KeyRelease>", self.on_key)
23
+ self.ui_entry.pack(side="left", fill="x", expand=1)
24
+
25
+ self.uiv_star = '>' # '>'=起始匹配 '*'=模糊匹配
26
+ self.ui_star = tk.Label(fr, text=self.uiv_star)
27
+ self.ui_star.pack(side="right")
28
+ self.ui_star.bind("<1>", self.on_star)
29
+
30
+ self.uiv_lstbox = tk.StringVar()
31
+ self.ui_lstbox = tk.Listbox(self, listvariable = self.uiv_lstbox, height=28, width=36)
32
+ self.ui_lstbox.pack(side="top", fill="both", expand=1)
33
+ self.ui_lstbox.bind("<ButtonRelease-1>", self.on_sel)
34
+
35
+ self.index_cur = index_home
36
+ self.on_key(None)
37
+ self.ui_entry.focus_set()
38
+
39
+ def on_label_click(self, event):
40
+ """
41
+ 点击index标签
42
+ """
43
+ print(self.history)
44
+ if len(self.history) < 1: return
45
+ k = self.history.pop()
46
+ print("pop", k)
47
+ self.uiv_entry.set(k)
48
+ self.on_key(None)
49
+
50
+ def on_index_double_click(self, event):
51
+ """
52
+ 双击index标签
53
+ """
54
+ pass
55
+
56
+ def on_star(self, event):
57
+ """
58
+ 点击星号标签
59
+ """
60
+ if self.uiv_star == '*': self.uiv_star = '>'
61
+ else: self.uiv_star = '*'
62
+ self.ui_star.config(text=self.uiv_star)
63
+ self.on_key(None)
64
+
65
+ def lookup_dict(self, s:str):
66
+ if not s:
67
+ return self.index_home
68
+ cnt = 0
69
+ a = []
70
+ s = s.lower()
71
+ for k in self.dict_index:
72
+ k_ = k.lower()
73
+ if self.uiv_star == '*':
74
+ if k_.find(s) < 0: continue
75
+ else:
76
+ if not k_.startswith(s): continue
77
+ a.append(k)
78
+ cnt += 1
79
+ if cnt >= 50: break
80
+ return sorted(a)
81
+
82
+ def on_key(self, _):
83
+ """
84
+ 用户输入新的key
85
+ """
86
+
87
+ k = self.ui_entry.get()
88
+ if _ is not None and k:
89
+ if self.history:
90
+ lastk = self.history[-1]
91
+ if k.startswith(lastk):
92
+ self.history[-1] = k
93
+ elif not lastk.startswith(k):
94
+ self.history.append(k)
95
+ else:
96
+ pass
97
+ else:
98
+ self.history.append(k)
99
+ print(self.history)
100
+
101
+ self.index_cur = self.lookup_dict(k)
102
+ self.ui_lstbox.delete(0, tk.END)
103
+ self.ui_lstbox.insert(0, *self.index_cur)
104
+ self.ui_lstbox.select_set(0)
105
+ self.on_sel()
106
+
107
+ def on_sel(self, _=None):
108
+ """
109
+ 用户选择列表中的项目
110
+ """
111
+ sel = self.ui_lstbox.curselection()
112
+ if not sel: return
113
+ i = sel[0]
114
+ k = self.index_cur[i]
115
+ if self.cb_command is not None:
116
+ self.cb_command(k)
117
+ else:
118
+ print("on_sel_key:", k)
@@ -0,0 +1,130 @@
1
+ # coding: utf8
2
+
3
+ # drag & drop items:
4
+ # see https://stackoverflow.com/questions/14459993/tkinter-listbox-drag-and-drop-with-python
5
+ # there are samples for selectmode MULTIPLE
6
+
7
+
8
+ import tkinter as tk
9
+ from tkinter_yzw.tk_dialog import TkYzwDialog
10
+
11
+
12
+ class _DlgItemEditor(TkYzwDialog):
13
+ def __init__(self, master, itemtext="", **ka):
14
+ super().__init__(master, title="item edit", **ka)
15
+
16
+ self.uiv_edit1 = tk.StringVar(value=itemtext)
17
+ tk.Entry(self, textvariable=self.uiv_edit1).pack(fill="both", expand=0)
18
+
19
+ fr = tk.Frame(self); fr.pack(side="top", pady=5, fill="both")
20
+
21
+ tk.Button(fr, text="OK", command=self.on_OK).pack(side="left", fill="both", expand=1)
22
+ tk.Button(fr, text="Cancel", command=self.on_Cancel).pack(side="left", fill="both", expand=1)
23
+
24
+ def on_OK(self):
25
+ self.destroy()
26
+ self.result = self.uiv_edit1.get()
27
+
28
+ def on_Cancel(self):
29
+ self.destroy()
30
+ self.result = None
31
+
32
+
33
+ class TkYzwListedit(tk.Listbox):
34
+ def __init__(self, master, items=None, cls_item_editor=None, **kw):
35
+ if items is None:
36
+ items = []
37
+ if cls_item_editor is None:
38
+ cls_item_editor = _DlgItemEditor
39
+ self.cls_item_editor = cls_item_editor
40
+
41
+ kw['selectmode'] = tk.SINGLE
42
+ if "listvariable" not in kw: kw['listvariable'] = tk.Variable()
43
+ tk.Listbox.__init__(self, master, kw)
44
+ self.var = self.cget('listvariable')
45
+ self.curIndex = None
46
+
47
+ for x in items:
48
+ self.insert(tk.END, x)
49
+ self.insert(tk.END, "+")
50
+
51
+ self.bind('<Button-1>', self.setCurrent)
52
+ self.bind('<B1-Motion>', self.shiftSelection)
53
+ self.bind('<Double-Button-1>', self.on_double1)
54
+ self.bind('<KeyPress>', self.on_key)
55
+
56
+ def setCurrent(self, event):
57
+ self.curIndex = self.nearest(event.y)
58
+
59
+ def shiftSelection(self, event):
60
+ i = self.nearest(event.y)
61
+ if i < self.curIndex:
62
+ x = self.get(i)
63
+ self.delete(i)
64
+ self.insert(i+1, x)
65
+ self.curIndex = i
66
+ elif i > self.curIndex:
67
+ x = self.get(i)
68
+ self.delete(i)
69
+ self.insert(i-1, x)
70
+ self.curIndex = i
71
+
72
+ def on_layout_save(self):
73
+ var = self.cget('listvariable')
74
+ a = list(self.getvar(var))
75
+ a.remove('+')
76
+ return a
77
+
78
+ def on_double1(self, ev):
79
+ # edit item
80
+ print("edit %r"%ev)
81
+ a_sel = self.curselection()
82
+ print(f"curselection {a_sel}")
83
+ if not a_sel: return
84
+ sel = a_sel[0]
85
+ text = self.get(sel)
86
+ print(f"get {text}")
87
+ if text == '+':
88
+ dlg = self.cls_item_editor(self.master)
89
+ result = dlg.run()
90
+ if result:
91
+ self.insert(sel, result)
92
+ else:
93
+ dlg = self.cls_item_editor(self, itemtext=text)
94
+ result = dlg.run()
95
+ if result:
96
+ self.delete(sel)
97
+ self.insert(sel, result)
98
+
99
+ def on_key(self, ev):
100
+ print("on_key %r"%ev)
101
+ a_sel = self.curselection()
102
+ print(f"curselection {a_sel}")
103
+ if ev.keysym == "Delete":
104
+ for sel in a_sel:
105
+ text = self.get(sel)
106
+ if text != '+':
107
+ self.delete(sel)
108
+
109
+
110
+ if __name__ == '__main__':
111
+ class Ui:
112
+ def __init__(self):
113
+ self.root = tk.Tk()
114
+ self.root.title("tk_listedit demo")
115
+
116
+ items = ["111", "2222", "33333"]
117
+ self.uix_le = TkYzwListedit(self.root, items=items, cls_item_editor=_DlgItemEditor)
118
+ self.uix_le.pack()
119
+
120
+ fr = tk.Frame()
121
+ fr.pack(side="top", fill='x')
122
+ tk.Button(fr, text="OK", command=self.on_ok, bg="#d0e0d0").pack(side="left")
123
+ tk.Button(fr, text="Exit", command=self.root.destroy, bg="#d0e0d0").pack(side="left")
124
+
125
+ def on_ok(self):
126
+ a = self.uix_le.on_layout_save()
127
+ print(a)
128
+
129
+ ui = Ui()
130
+ ui.root.mainloop()