【问题标题】:How to show this message when mouse stop鼠标停止时如何显示此消息
【发布时间】:2021-11-15 13:02:46
【问题描述】:

我想使用 python3 (tkinter) 来完成这个功能: 当鼠标停在某个地方时,将显示一条消息。 当鼠标移开时,消息会消失。

有关于这个功能的信息吗?

【问题讨论】:

  • 好吧,您可以绑定到"<Motion>" 事件以了解运动,但您必须使用计时器来检测鼠标运动何时停止。这不被视为“事件”。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: python python-3.x tkinter


【解决方案1】:

这个效果可以通过bindafter组合来实现。

使用bind 跟踪光标的运动,删除label

after 每 50 毫秒检查一次光标是否未移动,并使用 place 管理器显示 label

自从bind event.x and y 导致消息的定位偶尔出现错误以来,我已经更新了答案。 (显示在左上角)

追踪到event.x event.y,因此将其替换为

x = root.winfo_pointerx() - root.winfo_rootx()

y = root.winfo_pointery() - root.winfo_rooty()

此更新解决了问题,现在它按预期运行。

import tkinter as tk

root = tk.Tk()
label = tk.Label(root, text = "Message")
x = y = xx = yy = 0

def display(event):
    global x, y
    x = root.winfo_pointerx() - root.winfo_rootx()
    y = root.winfo_pointery() - root.winfo_rooty()
    label.place_forget()

def check(xx, yy):
    global x, y
    if x == xx and y == yy:
        label.place(x = x, y = y, anchor = "s")
    else:
        xx, yy = x, y
    root.after(50, check, xx, yy)

root.bind("<Motion>", display)
root.after(10, check, xx, yy)
root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2010-09-15
    • 2012-10-08
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多