嗯,我正在尝试用一个我不太了解的图片制作一个虚拟助手,我想知道是否有一种方法可以锁定和解锁助手的移动。
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()目前我有这个,但移动受阻
发布于 2021-05-11 18:01:03
您需要通过绑定<Button-1>和<B1-Motion>事件来手动执行移动:
from tkinter import *
can_drag = False
def toggle_drag(event):
global can_drag
can_drag = not can_drag
def on_click(event):
global dx, dy
if can_drag:
# save the distance between mouse position and the top-left corner of main window
dx, dy = event.x_root-main.winfo_rootx(), event.y_root-main.winfo_rooty()
def on_drag(event):
if can_drag:
# move the main window
main.geometry(f"+{event.x_root-dx}+{event.y_root-dy}")
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.bind("<Button-1>", on_click)
main.bind("<B1-Motion>", on_drag)
main.bind("<F2>", toggle_drag) # function key to lock and unlock movement
main.mainloop()https://stackoverflow.com/questions/67482920
复制相似问题