python tkinter - 如何让 ttk Notebook 标签页改变它们的顺序?
python tkinter - How can I make ttk Notebook tabs change their order?
是否可以这样做:
使用 ttk.Notebook
小部件?
是的,这是可能的。您必须将 B1-Motion
绑定到一个函数,然后使用 notebook.index("@x,y")
获取鼠标位置的选项卡索引。然后,您可以利用 notebook.insert()
在特定位置插入。
import tkinter as tk
from tkinter import ttk
def reorder(event):
try:
index = notebook.index(f"@{event.x},{event.y}")
notebook.insert(index, child=notebook.select())
except tk.TclError:
pass
root = tk.Tk()
root.geometry("500x500")
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)
notebook.bind("<B1-Motion>", reorder)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
notebook.add(frame1, text='Whosebug')
notebook.add(frame2, text='Github')
root.mainloop()
输出:
是否可以这样做:
使用 ttk.Notebook
小部件?
是的,这是可能的。您必须将 B1-Motion
绑定到一个函数,然后使用 notebook.index("@x,y")
获取鼠标位置的选项卡索引。然后,您可以利用 notebook.insert()
在特定位置插入。
import tkinter as tk
from tkinter import ttk
def reorder(event):
try:
index = notebook.index(f"@{event.x},{event.y}")
notebook.insert(index, child=notebook.select())
except tk.TclError:
pass
root = tk.Tk()
root.geometry("500x500")
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)
notebook.bind("<B1-Motion>", reorder)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
notebook.add(frame1, text='Whosebug')
notebook.add(frame2, text='Github')
root.mainloop()
输出: