How to reorder tabs by drag and drop in a Tkinter notebook

Viewed 279

I've created a notebook which has tabs, and I want to rearrange the tabs by dragging and dropping.

I tried creating an example where the button, which is not created in a notebook, is getting dragged, but not the tabs, which are created in the notebook.

import tkinter as tk
from tkinter import ttk
import Tkdnd


def make_draggable(widget):
    widget.bind("<Button-1>", on_drag_start)
    widget.bind("<B1-Motion>", on_drag_motion)

def on_drag_start(event):
    print "on_drag_start: "
    print "widget event: ", event
    print "widget: ", event.widget
    widget = event.widget
    widget._drag_start_x = event.x
    widget._drag_start_y = event.y

def on_drag_motion(event):
    print "on_drag_motion: "
    print "widget event: ", event

    widget = event.widget
    x = widget.winfo_x() - widget._drag_start_x + event.x
    y = widget.winfo_y() - widget._drag_start_y + event.y
    print "tuple: ", x, y
    widget.place(x=x, y=y)

root = tk.Tk()
root.title("Tab Widget")

btn = tk.Button(text="testing")
btn.pack()
make_draggable(btn)
tabControl = ttk.Notebook(root)

tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)

tabControl.add(tab1, text='Tab 1')
tabControl.add(tab2, text='Tab 2')

tabControl.pack(expand=1, fill="both")
make_draggable(tab1)
make_draggable(tab2)


tab1.bind("<Button-1>", on_drag_start)
tab1.bind("<B1-Motion>", on_drag_motion)

tab2.bind("<Button-1>", on_drag_start)
tab2.bind("<B1-Motion>", on_drag_motion)

ttk.Label(tab1, text="Welcome to \GeeksForGeeks").grid(column=0, row=0, padx=30, pady=30)
ttk.Label(tab2, text="Lets dive into the\world of computers").grid(column=0, row=0, padx=30, pady=30)


root.mainloop()
0 Answers
Related