鼠标悬停在 tkinter 按钮上

时间:2021-03-06 11:09:38

标签: python tkinter button mouseover

将光标悬停在按钮上是否可以获得弹出选项(代码中的pop函数)?

import tkinter as tk
from tkinter import Tk, BOTH, Menu

def pop(bt):
    try:
        x = bt.winfo_rootx()+238
        y = bt.winfo_rooty()+10
        popup.tk_popup(x, y, 0)
    finally:
        popup.grab_release()


root = tk.Tk()

popup = Menu(root, tearoff=0,relief='raised')
popup.add_command(label="About")
popup.add_command(label="User manual")
popup.add_command(label="Contact us")

button1 =tk.Button(root, text="HELP",height=3,width=26,command=lambda: controller.show_frame(HelpPage))                   
button1.configure(command = lambda: pop(button1))
button1.place(x=0,y=0
              )
root.mainloop()

.

button1.bind('<Enter>',pop(button1)) #gives the following output without the mouse cursor over that button.

enter image description here

3 个答案:

答案 0 :(得分:0)

试试这个:

import tkinter as tk

window = None

def leave_window(event):
    global window
    if 0 < root.winfo_pointerx() - root.winfo_rootx() < button.winfo_width():
        if 0 < root.winfo_pointery() - root.winfo_rooty() < button.winfo_height():
            # Mouse still over button
            return None
    if window is not None:
        window.destroy()
        window = None

def create_window(event):
    global window
    if window is not None:
        # The window is already open
        return None
    window = tk.Toplevel(root)
    window.overrideredirect(True)
    label = tk.Label(window, text="Text", bg="black", fg="white")
    label.pack()
    window.update()
    # Move the window to the cursor's
    x = root.winfo_pointerx()
    y = root.winfo_pointery()-window.winfo_height()
    window.geometry("+%i+%i" % (x, y))
    window.bind("<Leave>", leave_window)


root = tk.Tk()
button = tk.Button(root, text="-------- Hover the mouse here --------")
button.pack(fill="both", expand=True)
button.bind("<Enter>", create_window)
button.bind("<Leave>", leave_window)

root.mainloop()

我绑定到 <Enter><Leave> 来检查鼠标是否在按钮上。如果是,它会创建一个带有名为 text 的标签的窗口。您可以将其更改为显示菜单。

有关更多答案,请查看here

答案 1 :(得分:0)

请用鼠标输入绑定您的Button并调用pop函数。 看看这个代码:

button1.bind('<Enter>', pop)

控制你的弹窗位置,并在 '' case 上销毁弹窗再次绑定它并调用将销毁弹窗的自定义函数。

答案 2 :(得分:0)

您需要 <Enter><Leave> 绑定序列来映射和取消映射菜单。 Tkinter Menu 有两个方法 postunpost,其中 post 显示给定坐标处的菜单,unpost 将其隐藏。不幸的是,我无法测试它,因为取消发布功能在 macOS 或 Linux [refer to this link for the same] 上不起作用。我还更改了 xy 坐标以映射小部件中心的菜单 (Button),如果需要,可以更改它。


这是完整的示例代码。

import tkinter as tk
from tkinter import Tk, BOTH, Menu


def pop(evt):
    but = evt.widget
    if str(evt.type) == "Enter":
        # Map the menu in the center of the width.
        x = but.winfo_rootx() + int(but.winfo_width()/2)
        y = but.winfo_rooty() + int(but.winfo_height()/2)
        popup.tk_popup(x, y)

    elif str(evt.type) == "Leave":
        popup.unpost()


root = tk.Tk()
root.geometry("300x300")

popup = Menu(root, tearoff=0, relief='raised')
popup.add_command(label="About")
popup.add_command(label="User manual")
popup.add_command(label="Contact us")

button1 = tk.Button(root, text="HELP", height=3, width=26)
button1.bind('<Enter>', pop)
button1.bind('<Leave>', pop)
button1.pack(pady=100)

root.mainloop()

就像说的那样,unpost 不适用于 macOS 或 Linux,所以我无法 100% 测试示例代码,但它应该可以正常工作。