Python 3.x tkinter中的锁定和解锁运动
嗯,我正在尝试用一个我对tkinter不太了解的图像制作一个虚拟助手,我想知道是否有一种方法可以锁定和解锁助手的移动Python 3.x tkinter中的锁定和解锁运动,python-3.x,tkinter,Python 3.x,Tkinter,嗯,我正在尝试用一个我对tkinter不太了解的图像制作一个虚拟助手,我想知道是否有一种方法可以锁定和解锁助手的移动 import tkinter from tkinter import * main=Tk() photo=PhotoImage(file='image.PNG') Label(main,image=photo,bg='grey').pack() main.wm_attributes("-transparentcolor", 'grey') main.wm_a
import tkinter
from tkinter import *
main=Tk()
photo=PhotoImage(file='image.PNG')
Label(main,image=photo,bg='grey').pack()
main.wm_attributes("-transparentcolor", 'grey')
main.wm_attributes("-topmost", True)
main.overrideredirect(1)
main.mainloop()
目前我有此功能,但移动被阻止您需要通过绑定
和
事件手动执行移动:
从tkinter导入*
can_drag=False
def切换_拖动(事件):
全局可拖动
可以拖动=不能拖动
单击时的def(事件):
全局dx,dy
如果可以拖动:
#保存鼠标位置与主窗口左上角之间的距离
dx,dy=event.x_root-main.winfo_rootx(),event.y_root-main.winfo_rooty()
拖动def(事件):
如果可以拖动:
#移动主窗口
几何体(f“{event.x_root-dx}+{event.y_root-dy}”)
main=Tk()
photo=PhotoImage(file='image.PNG')
标签(main,image=photo,bg='grey').pack()
main.wm_属性(“-transparentcolor”,“灰色”)
main.wm_属性(“-toppost”,True)
main.overrideredirect(1)
main.bind(“,单击时)
main.bind(“,拖动时)
main.bind(“,toggle_drag)#锁定和解锁移动的功能键
main.mainloop()
运动是什么意思?此外,您的代码所做的一切似乎都是创建一个无法与之交互的不可见窗口,以及该图像的用途?@matiss OP code将显示没有窗口边框的透明图像。OP想要的是能够拖动/移动图像。@acw1668我了解到,在我不再能够编辑代码后,已经在研究解决方案。建议使用字典来存储dx
和dy
,以免使用全局
(因为它是“邪恶的”)