Skip to content
Advertisement

Make Tkinter Notebook draggable

A similar question was asked back in ’15 Make Tkinter Notebook be Draggable to Another View but that was a while ago and that also asked about re-binding the window.

I was wondering how I would make a notebook draggable, even if is just to reorder the tabs.

Any advice would be helpful and please let me know if this question has been answered

Advertisement

Answer

Tab dragging has been implemented in TCL: https://wiki.tcl-lang.org/page/ttk::notebook, https://wiki.tcl-lang.org/page/Drag+and+Drop+Notebook+Tabs. It can either be translated to python or evaluated through .tk.eval().

For the second solution, one can put the TCL tab dragging code from the first link (except the last block which creates an example notebook) in a string and evaluate it with root.tk.eval(TCL_CODE). Subsequently created notebooks will have tab dragging:

import tkinter as tk
from tkinter import ttk

tcl_code = """
put here the code from the dragging tab code section of https://wiki.tcl-lang.org/page/ttk::notebook
"""

root = tk.Tk()
root.tk.eval(tcl_code)

nb = ttk.Notebook(root)
nb.pack()
for i in range(10):
    nb.add(ttk.Frame(nb, width=100, height=100), text=f"Tab {i}")

root.mainloop()

I also implemented tab dragging with animations (seeing the dragged tab move) as part of a larger project, the source code is available here.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement