【问题标题】:python tkinter - How can I make ttk Notebook tabs change their order?python tkinter - 如何让 ttk Notebook 选项卡更改其顺序?
【发布时间】:2021-09-17 12:48:07
【问题描述】:

是否可以这样做:

带有ttk.Notebook 小部件?

【问题讨论】:

    标签: python tkinter tabs ttk


    【解决方案1】:

    是的,这是可能的。您必须将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='Stackoverflow')
    notebook.add(frame2, text='Github')
    
    root.mainloop()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2015-03-20
      • 2018-08-19
      相关资源
      最近更新 更多