我试图在tkinter中使用拖放来交换网格布局中两个小部件的位置。在鼠标按钮RELEASE-1上,我想获得释放鼠标的小部件(myTextLabel2),这样我就可以使用单击的小部件(myTextLabel1)的行和列更改该小部件的行和列
我试着搜索在x,y位置获取小部件的方法,我能找到的是相反的,获取小部件的位置,而不是相反
以下是我目前的代码:
from tkinter import *
from functools import partial
def changeOrder(widget1,widget2):
widget1.grid(row=1,column=0)
widget2.grid(row=0,column=0)
def drag_start(event):
print(event.x)
def drag_motion(event):
x = event.x
y = event.y
def drag_release(event):
x = event.x
y = event.y
#I want to get the widget at position x,y, should be myTextLabel2
#Then swap the rows and columns for myTextLabel2 and myTextLabel1
root = Tk()
myTextLabel1 = Label(root,text="Label 1")
myTextLabel1.grid(row=0,column=0,padx=5,pady=5,sticky=E+W+S+N)
myTextLabel1.bind("<Button-1>",drag_start)
myTextLabel1.bind("<B1-Motion>",drag_motion)
myTextLabel1.bind("<ButtonRelease-1>",drag_release)
myTextLabel2 = Label(root,text="Label 2")
myTextLabel2.grid(row=1,column=0,padx=5,pady=5,sticky=E+W+S+N)
myButton = Button(root,text="Change order",command=partial(changeOrder,myTextLabel1,myTextLabel2))
myButton.grid(row=3,column=0,padx=5,pady=5,sticky=E+W+S+N)
root.mainloop()
你可以试试这样的
root.bind("<Button-1>",on_click)
已用于使用<Button-1>
事件绑定整个窗口,如果单击的小部件是Label
的实例,则函数on_click
将添加其他2个绑定。标签的grid_info()
被存储并相应地传递李>drag_motion
使用place
根据运动更改小部件的位置,并使用lift
方法将小部件保持在顶部李>drag_release
使用lower
方法降低小部件,然后使用winfo_pointerxy
方法获取光标的当前位置,并将返回值传递给winfo_containing
方法,该方法在鼠标指针下返回小部件李>通过将可交换的小部件存储在列表中,然后检查小部件是否位于该列表中,可以替换
isinstance
逻辑PS:由于对代码所做的更改,您原始帖子中的按钮将无法工作
相关问题 更多 >
编程相关推荐