[英]How can i drag and drop multiple widgets at the same time i can drag my labels but want to drag them at the same time
from tkinter import *
def vp_start_gui():
root = Tk()
top = Toplevel1 (root)
root.mainloop()
class Toplevel1:
def __init__(self, top=None):
top.geometry("300x300")
I use all of your funtions they work great the way you had them but if I put it a class is doesent work well我使用了你所有的功能,它们的工作方式和你的方式一样好,但是如果我把它说成 class 就不能正常工作
if I select individually it jumps about half a screen def select_and_drag(widgets_list): """Drags all the widgets together in the given list with mouse point.如果我 select 单独跳跃大约半个屏幕 def select_and_drag(widgets_list): """用鼠标点将给定列表中的所有小部件一起拖动。
Args:
widgets_list (list, tuple): Takes list of widgets
to be moved together."""
def set_drag(evt):
"""Set iniitial points."""
for w in widgets_list:
w._drag_x = evt.x
w._drag_y = evt.y
def on_drag(evt):
"""Drags with mouse."""
for w in widgets_list:
x = w.winfo_x() - w._drag_x + evt.x
y = w.winfo_y() - w._drag_y + evt.y
w.place(x=x, y=y)
for wid in widgets_list:
wid.bind("<Button-1>", set_drag, '+')
wid.bind("<B1-Motion>", on_drag, '+')
self.Label1_3 = Label(top)
self.Label1_3.place(relx=0.687, rely=0.183, height=36, width=124)
self.Label1_3.configure(text='''Height''', bg="brown")
self.Label1 = Label(top, text="try")
self.Label1.pack()
select_and_drag((self.Label1_3, self.Label1))
select_and_drag((self.Label1,))
if __name__ == '__main__':
vp_start_gui()
Yes, it is possible and you are almost there with your code.是的,这是可能的,您的代码几乎就在那里。 I just modified and improved your drag functionality, I made one function
select_and_drag(widget_list)
which takes a list of widgets and makes a set of them together, which means moving one widget will make all the widgets in that set move along.我刚刚修改并改进了您的拖动功能,我制作了一个 function
select_and_drag(widget_list)
,它采用小部件列表并将它们组合在一起,这意味着移动一个小部件将使该集合中的所有小部件移动。
Here is the function.这是 function。
def select_and_drag(widgets_list):
"""Drags all the widgets together
in the given list with mouse point.
Args:
widgets_list (list, tuple): Takes list of widgets
to be moved together."""
def set_drag(evt):
"""Set iniitial points."""
for w in widgets_list:
w._drag_x = evt.x
w._drag_y = evt.y
def on_drag(evt):
"""Drags with mouse."""
for w in widgets_list:
x = w.winfo_x() - w._drag_x + evt.x
y = w.winfo_y() - w._drag_y + evt.y
w.place(x=x, y=y)
for wid in widgets_list:
wid.bind("<Button-1>", set_drag, '+')
wid.bind("<B1-Motion>", on_drag, '+')
How to use it?如何使用它?
More than one set can be created with different widgets from this function.可以使用来自此 function 的不同小部件创建多个集合。 You can get an idea from this example below, move each SET and see for yourself.
你可以从下面的这个例子中得到一个想法,移动每个SET并自己看看。
from tkinter import *
root = Tk()
root.geometry("600x350")
# SET 1
lbl1 = Label(root, text='SET1', fg='red')
lbl1.pack()
lbl2 = Label(root, text='SET1', fg='red')
lbl2.pack()
# Common for SET1 and SET3
lbl3 = Label(root, text='SET1 and SET3',
bg='pink', fg='red')
lbl3.pack()
# SET2
lbl4 = Label(root, text='SET2', bg='lightblue')
lbl4.pack()
lbl5 = Label(root, text='SET2', bg='lightblue')
lbl5.pack()
# SET2
lbl6 = Label(root, text='SET3', bg='pink')
lbl6.pack()
# Make SETS.
select_and_drag((lbl1, lbl2, lbl3))
select_and_drag((lbl4, lbl5))
select_and_drag((lbl3, lbl6))
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.