首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >tkinter中锁定和解锁的运动

tkinter中锁定和解锁的运动
EN

Stack Overflow用户
提问于 2021-05-11 16:17:19
回答 1查看 20关注 0票数 0

嗯,我正在尝试用一个我不太了解的图片制作一个虚拟助手,我想知道是否有一种方法可以锁定和解锁助手的移动。

代码语言:javascript
运行
复制
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()

目前我有这个,但移动受阻

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-11 18:01:03

您需要通过绑定<Button-1><B1-Motion>事件来手动执行移动:

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67482920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档