简体   繁体   中英

python tkinter - How can I make ttk Notebook tabs change their order?

Is it possible to do this:

在此处输入图片说明

with ttk.Notebook widget?

Yes, it is possible. You have to bind B1-Motion to a function, then use notebook.index("@x,y") to get the index of the tab at mouse position. You can then make use of notebook.insert() to insert at a particular position.

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='Stackoverflow')
notebook.add(frame2, text='Github')

root.mainloop()

output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM