阅读量:0
import tkinter as tk def on_drag_start(event): widget.lift() # Colocar o widget no topo quando arrastado widget.start_x = event.x widget.start_y = event.y def on_drag_motion(event): x = widget.winfo_x() - widget.start_x + event.x y = widget.winfo_y() - widget.start_y + event.y widget.place(x=50, y=50) def on_drag_start_new(event): widget_new.lift() widget_new.start_x = event.x widget_new.start_y = event.y def on_drag_motion_new(event): x = widget_new.winfo_x() - widget_new.start_x + event.x y = widget_new.winfo_y() - widget_new.start_y + event.y widget_new.place(x=x, y=y) def create_draggable_widget(): global widget global widget_new root = tk.Tk() widget = tk.Label(root, text="test", bg="lightblue") widget.bind("<ButtonPress-1>", on_drag_start) widget.bind("<B1-Motion>", on_drag_motion) widget.place(x=50, y=50, anchor="nw") widget_new = tk.Label(root, text="test", bg="lightblue") widget_new.bind("<ButtonPress-1>", on_drag_start_new) widget_new.bind("<B1-Motion>", on_drag_motion_new) widget_new.place(x=50, y=50, anchor="nw") root.mainloop() create_draggable_widget()