I have a request if you don't mind. My main goal is to code a reorder list items with drag and drop feature in Tkinter (just a simple code example). I prefer it to include different widgets , for example : buttons, images, labels ...
In my case I did an attempt to drag and drop list of buttons but this is not what I want to reach.
If you want to have an idea on how It gonna look like. This is the video which illustrates what I wanna say.
https://youtu.be/LDej-JtE1OY?t=13
This is the code I attempted to make:
import tkinter as tk
def on_button_press(event):
global item_being_dragged
item_being_dragged = event.widget
def on_button_release(event):
global item_being_dragged
item_being_dragged.place_forget()
item_being_dragged.place(x=event.x_root, y=event.y_root)
item_being_dragged = None
root = tk.Tk()
item_being_dragged = None
for i in range(10):
b = tk.Button(root, text=str(i))
b.bind("<ButtonPress-1>", on_button_press)
b.bind("<B1-Motion>", lambda e: item_being_dragged.place(x=e.x_root, y=e.y_root))
b.bind("<ButtonRelease-1>", on_button_release)
b.pack()
I expect (if you can edit my code and turn it to the result shown in the YouTube video link.I will really appreciate that because my goal is just to know how the algorithm of this feature is written and understand it to advance in my journey of learning Tkinter.**
source https://stackoverflow.com/questions/75402417/reorder-list-items-with-drag-and-drop-feature-in-tkinter
Comments
Post a Comment