├── LICENSE ├── README.md ├── dragwindow.py ├── remove.gif ├── search_box.py ├── tabview.py └── test.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tkinter-tabview 2 | 3 | > ttk中虽然添加了`Notebook`,但其功能过于简单,无法支持双击创建选项卡,删除选项卡等功能,于是自定义了tabview,有需要的朋友,可以参考在tkinter中自定义view的方法,自定义自己的view 4 | 5 | ![](https://github.com/arcticfox1919/ImageHosting/blob/master/tabview.gif?raw=true) 6 | 7 | 8 | 使用方法 9 | ```python 10 | import tkinter as tk 11 | from tkinter import messagebox 12 | from tabview import TabView 13 | 14 | 15 | def create_body(): 16 | global body 17 | return tk.Label(body, text="this is body") 18 | 19 | 20 | def select(index): 21 | print("current selected -->", index) 22 | 23 | 24 | def remove(index): 25 | print("remove tab -->", index) 26 | if messagebox.askokcancel("标题", "确定要关闭该选项卡吗?"): 27 | return True 28 | else: 29 | return False 30 | 31 | 32 | root = tk.Tk() 33 | root.geometry("640x300") 34 | 35 | tab_view = TabView(root, generate_body=create_body, 36 | select_listen=select, remove_listen=remove) 37 | 38 | body = tab_view.body 39 | 40 | label_1 = tk.Label(tab_view.body, text="this is tab1") 41 | label_2 = tk.Label(tab_view.body, text="this is tab2") 42 | 43 | # 第一个参数是向body中添加的widget, 第二个参数是tab标题 44 | tab_view.add_tab(label_1, "tabs1") 45 | tab_view.add_tab(label_2, "tabs2") 46 | 47 | # TabView需要向x、y方向填充,且expand应设置为yes 48 | tab_view.pack(fill="both", expand='yes', pady=2) 49 | 50 | root.mainloop() 51 | ``` 52 | 53 | # tkinter-DragWindow 54 | 实现桌面可拖拽小挂件 55 | 56 | ![预览](https://github.com/arcticfox1919/ImageHosting/blob/master/DragWindow.gif?raw=true) 57 | 58 | 使用方法 59 | 60 | ``` 61 | # 导入DragWindow类 62 | root = DragWindow() 63 | root.set_window_size(200, 200) 64 | root.set_display_postion(500, 400) 65 | tk.Button(root, text="Exit", command=root.quit).pack(side=tk.BOTTOM) 66 | 67 | root.mainloop() 68 | ``` 69 | 70 | 了解更多,查看本人博客 [传送门](https://arcticfox.blog.csdn.net/article/details/89605240) 71 | 72 | # tkinter-SearchBox 73 | 74 | 实现搜索框控件 75 | 76 | ![2021-03-29-001](https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/2021-03-29-001.png) 77 | 78 | [视频讲解地址](https://www.bilibili.com/video/BV1YB4y1w75T) 79 | 80 | ```python 81 | import tkinter as tk 82 | 83 | from search_box import SearchBox 84 | 85 | g_list = ["Aaron", "Abbott", "dart", "Abel", "Abner", "Abraham", "Absalom", "Ace", "Adair"] 86 | 87 | 88 | # 输入回调 89 | def callback(text): 90 | print(text) 91 | if not text: 92 | search.update(None) 93 | return 94 | 95 | tmp = [] 96 | for it in g_list: 97 | if it.startswith(text): 98 | tmp.append(it) 99 | 100 | # 更新弹出框内容 101 | search.update(tmp) 102 | 103 | 104 | root = tk.Tk() 105 | 106 | tk.Label(text="搜索框").pack() 107 | 108 | search = SearchBox(root, callback=callback) 109 | search.pack(side=tk.LEFT, padx=5, pady=5) 110 | 111 | root.mainloop() 112 | ``` 113 | 114 | -------------------------------------------------------------------------------- /dragwindow.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | 3 | 4 | class DragWindow(tk.Tk): 5 | root_x, root_y, abs_x, abs_y = 0, 0, 0, 0 6 | width, height = None, None 7 | 8 | def __init__(self, topmost=True, alpha=0.4, bg="gray", width=None, height=None): 9 | super().__init__() 10 | self["bg"] = bg 11 | self.width, self.height = width, height 12 | self.overrideredirect(True) 13 | self.wm_attributes("-alpha", alpha) # 透明度 14 | self.wm_attributes("-toolwindow", True) # 置为工具窗口 15 | self.wm_attributes("-topmost", topmost) # 永远处于顶层 16 | self.bind('', self._on_move) 17 | self.bind('', self._on_tap) 18 | 19 | def set_display_postion(self, offset_x, offset_y): 20 | self.geometry("+%s+%s" % (offset_x, offset_y)) 21 | 22 | def set_window_size(self, w, h): 23 | self.width, self.height = w, h 24 | self.geometry("%sx%s" % (w, h)) 25 | 26 | def _on_move(self, event): 27 | offset_x = event.x_root - self.root_x 28 | offset_y = event.y_root - self.root_y 29 | 30 | if self.width and self.height: 31 | geo_str = "%sx%s+%s+%s" % (self.width, self.height, 32 | self.abs_x + offset_x, self.abs_y + offset_y) 33 | else: 34 | geo_str = "+%s+%s" % (self.abs_x + offset_x, self.abs_y + offset_y) 35 | self.geometry(geo_str) 36 | 37 | def _on_tap(self, event): 38 | self.root_x, self.root_y = event.x_root, event.y_root 39 | self.abs_x, self.abs_y = self.winfo_x(), self.winfo_y() 40 | 41 | -------------------------------------------------------------------------------- /remove.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arcticfox1919/tkinter-tabview/c30aaad22f4ccc6f6f2b7bf147f4858ddaa1d57d/remove.gif -------------------------------------------------------------------------------- /search_box.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import Toplevel, Listbox 3 | from tkinter import Entry 4 | from tkinter import StringVar 5 | 6 | 7 | class SearchBox(Entry): 8 | 9 | def __init__(self, master=None, callback=None, lines=8, cnf={}, **kw): 10 | super().__init__(master, cnf, **kw) 11 | self.str_var = tk.StringVar() 12 | self["textvariable"] = self.str_var 13 | self.str_var.trace('w', self._callback) 14 | self.window = None 15 | self.lines = lines 16 | self.callback = callback 17 | self.master = master 18 | self.list_var = StringVar() 19 | self.prev_text = "" 20 | 21 | def _callback(self, *_): 22 | current_text = self.str_var.get() 23 | if current_text != self.prev_text: 24 | self.prev_text = current_text 25 | self.callback(current_text) 26 | 27 | def update(self, item_list): 28 | if item_list and self.window: 29 | self.list_var.set(item_list) 30 | elif not item_list and self.window: 31 | self._hide() 32 | elif item_list and not self.window: 33 | self._show() 34 | self.list_var.set(item_list) 35 | 36 | def _show(self): 37 | self.window = Toplevel() 38 | self.window.transient(self.master) 39 | self.window.overrideredirect(True) 40 | self.window.attributes("-topmost", 1) 41 | self.window.attributes("-alpha", 0.9) 42 | x = self.winfo_rootx() 43 | y = self.winfo_rooty() + self.winfo_height() + 6 44 | self.window.wm_geometry("+%d+%d" % (x, y)) 45 | self._create_list() 46 | # self.window.mainloop() 47 | 48 | def _listbox_click(self, event): 49 | widget = event.widget 50 | cur_item = widget.get(widget.curselection()) 51 | self.str_var.set(cur_item) 52 | self._hide() 53 | 54 | def _create_list(self): 55 | list_box = Listbox(self.window, selectmode=tk.SINGLE, listvariable=self.list_var, height=self.lines) 56 | list_box.bind('<>', self._listbox_click) 57 | list_box.pack(fill=tk.BOTH, expand=tk.YES) 58 | 59 | def _hide(self): 60 | if self.window: 61 | self.window.destroy() 62 | self.window = None 63 | -------------------------------------------------------------------------------- /tabview.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | import tkinter.ttk as ttk 3 | 4 | 5 | class TabView(tk.Frame): 6 | _tabs = [] 7 | _tab_view = [] 8 | _tab_text = [] 9 | _current = 0 # 当前选中tab的索引 10 | _generate_func = None 11 | _select_listen = None 12 | _remove_listen = None 13 | 14 | ''' 15 | select_listen: tab选中事件回调函数,回调函数包含一个index参数 16 | remove_listen: tab删除事件回调函数,返回值必须是一个布尔值,True表示删除,反之不删除 17 | generate_body: 生成body控件的回调函数,返回值应该是一个widget,用于添加到body中 18 | ''' 19 | 20 | def __init__(self, master=None, select_listen=None, 21 | remove_listen=None, generate_body=None, cnf={}, **kw): 22 | super().__init__(master, cnf, **kw) 23 | # 选项卡 24 | self._top = tk.Frame(self) 25 | self._top.bind("", self._tab_add) 26 | self._top.pack(fill="x") 27 | 28 | # 分割线 29 | ttk.Separator(self, orient=tk.HORIZONTAL).pack(fill="x") 30 | 31 | # 主体view 32 | self._bottom = tk.Frame(self) 33 | self._bottom.pack(fill="both", expand="yes") 34 | self._photo = tk.PhotoImage(file="remove.gif") 35 | 36 | # 事件回调 37 | if select_listen: 38 | if callable(select_listen): 39 | self._select_listen = select_listen 40 | else: 41 | raise Exception("select_action is not callable") 42 | 43 | if remove_listen: 44 | if callable(remove_listen): 45 | self._remove_listen = remove_listen 46 | else: 47 | raise Exception("remove_action is not callable") 48 | 49 | if generate_body: 50 | if callable(generate_body): 51 | self._generate_func = generate_body 52 | else: 53 | raise Exception("generate_body is not callable") 54 | 55 | # 添加选项卡 56 | def add_tab(self, view, text): 57 | self._add_body(view) 58 | self._create_tab(text) 59 | 60 | # 删除选项卡 61 | def remove_tab(self, index): 62 | self._tab_text.pop(index) 63 | 64 | self._tabs[index].destroy() 65 | self._tab_view[index].destroy() 66 | 67 | self._tabs.pop(index) 68 | self._tab_view.pop(index) 69 | 70 | if index > 0: 71 | self._current = index - 1 72 | self._active() 73 | 74 | @property 75 | def body(self): 76 | return self._bottom 77 | 78 | def _create_tab(self, text): 79 | # 选项卡单元 80 | tab_container = tk.Frame(self._top, relief=tk.RAISED, bd=1) 81 | 82 | # 标题、删除按钮 83 | _tab_text_view = tk.Label(tab_container, text=text, padx=8) 84 | _tab_remove_view = tk.Label(tab_container, image=self._photo) 85 | 86 | _tab_text_view.bind("", self._tab_click) 87 | _tab_remove_view.bind("", self._tab_remove) 88 | 89 | _tab_text_view.pack(side=tk.LEFT) 90 | _tab_remove_view.pack(side=tk.LEFT) 91 | tab_container.pack(side=tk.LEFT) 92 | self._tab_text.append(_tab_text_view) 93 | self._tabs.append(tab_container) 94 | self._active() 95 | 96 | def _add_body(self, view): 97 | self._tab_view.append(view) 98 | view.place(relwidth=1.0, relheight=1.0) 99 | 100 | # 刷新当前激活状态 101 | def _active(self): 102 | for i, item in enumerate(self._tabs): 103 | if i == self._current: 104 | item.config(relief=tk.RAISED, bd=3) 105 | # lift方法,让当前widget处于最顶层 106 | self._tab_view[self._current].lift() 107 | else: 108 | item.config(bd=1) 109 | 110 | def _tab_add(self, event): 111 | self._current = len(self._tabs) 112 | if not self._generate_func: 113 | self.add_tab(tk.Frame(self.body), "Untitled") 114 | else: 115 | self.add_tab(self._generate_func(), "Untitled") 116 | 117 | # 删除事件回调 118 | def _tab_remove(self, event): 119 | current_widget = event.widget.winfo_parent() 120 | select_index = self._index(current_widget) 121 | 122 | is_remove = True 123 | if self._remove_listen: 124 | is_remove = self._remove_listen(select_index) 125 | 126 | if is_remove: 127 | if select_index != -1: 128 | self.remove_tab(select_index) 129 | 130 | # 选项卡点击事件回调 131 | def _tab_click(self, event): 132 | current_widget = event.widget.winfo_parent() 133 | select_index = self._index(current_widget) 134 | if select_index != -1: 135 | self._current = select_index 136 | self._active() 137 | 138 | if self._select_listen: 139 | self._select_listen(select_index) 140 | 141 | # 返回当前选项卡的索引 142 | def _index(self, el): 143 | index = [i for i, x in enumerate(self._tabs) if str(x) == el] 144 | return index[0] if index else -1 145 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import messagebox 3 | from tabview import TabView 4 | 5 | 6 | def create_body(): 7 | global body 8 | return tk.Label(body, text="this is body") 9 | 10 | 11 | def select(index): 12 | print("current selected -->", index) 13 | 14 | 15 | def remove(index): 16 | print("remove tab -->", index) 17 | if messagebox.askokcancel("标题", "确定要关闭该选项卡吗?"): 18 | return True 19 | else: 20 | return False 21 | 22 | 23 | root = tk.Tk() 24 | root.geometry("640x300") 25 | 26 | tab_view = TabView(root, generate_body=create_body, 27 | select_listen=select, remove_listen=remove) 28 | 29 | body = tab_view.body 30 | 31 | label_1 = tk.Label(tab_view.body, text="this is tab1") 32 | label_2 = tk.Label(tab_view.body, text="this is tab2") 33 | 34 | # 第一个参数是向body中添加的widget, 第二个参数是tab标题 35 | tab_view.add_tab(label_1, "tabs1") 36 | tab_view.add_tab(label_2, "tabs2") 37 | 38 | # TabView需要向x、y方向填充,且expand应设置为yes 39 | tab_view.pack(fill="both", expand='yes', pady=2) 40 | 41 | root.mainloop() 42 | --------------------------------------------------------------------------------