简体   繁体   English

如何同时拖放多个小部件我可以拖动标签但想同时拖动它们

[英]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.

相关问题 如何拖放带有自定义项目小部件的QListWidget项目? - How can I drag drop QListWidget items with custom item widgets? 如何将xml文件拖放到Qtableview中并同时解析xml - How to drag a xml file and drop it in Qtableview and parse the xml at the same time 如何将 QListview 中的图片添加到 QGraphicsView? (带拖放) - How can I add pictures from QListview to QGraphicsView? (with Drag Drop) tkinter - 如何拖放小部件? - tkinter - How to drag and drop widgets? 我可以像将.mat文件拖放到Matlab中一样,将pickle文件拖放到PyCharm中吗? - Can I drag and drop pickle files into PyCharm the same way I can d-n-d .mat files into Matlab? "<i>How can I run my Python script as a &quot;drag and drop&quot; icon on the dock?<\/i>如何将我的 Python 脚本作为 Dock 上的“拖放”图标运行?<\/b> <i>(MacOS)<\/i> (苹果系统)<\/b>" - How can I run my Python script as a "drag and drop" icon on the dock? (MacOS) 为什么我不能使用 Python 在 Selenium Chromedriver 中拖放? - Why can't I drag and drop in Selenium Chromedriver with Python? 我可以从其他窗口拖放吗? Python Selenium - Can i use drag and drop from other window? Python Selenium 我想在 ldap 中同时修改多个值。 我怎样才能做到这一点? - I want to modify multiple values at the same time in ldap. How can I do that? 如何拖放具有附加属性的 QListWidgetItem 的子 class 实例? - How can I Drag and Drop instances of a child class of QListWidgetItem, which have additional attributes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM