【问题标题】:How to open a menu tied to ttk.Menubutton when hovering over it?悬停在其上时如何打开与 ttk.Menubutton 相关的菜单?
【发布时间】:2020-04-21 07:20:42
【问题描述】:

本来想在Tkinter做一个自定义菜单栏,但是因为无法调整,只好做拐杖。我从FrameButtonMenubutton 制作了一个自定义菜单。但是我遇到了一个小问题 - 将鼠标悬停在 ttk.Menubutton 上时无法打开菜单。也就是说,当悬停在 Menubutton 上时,我需要打开附加到此按钮的菜单(模拟单击 Menubutton)。如何实现?

代码

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")


style = ttk.Style(root)

fr = ttk.Frame(root)

btn_menu = ttk.Menubutton(fr, text='fegvd')
btn_menu.grid(row=0, column=0)

btn =ttk.Button(fr, text='grfbvgfev')
btn.grid(row=0, column=1)

btn_menu_st = ttk.Menubutton(fr, text='Gds')
btn_menu_st.grid(row=0, column=2)

fr.pack(fill='x')


file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')

style = tk.Menu(btn_menu_st, tearoff=0, foreground='white')
style.add_command(label='Ugu')

btn_menu.configure(menu=file)
btn_menu_st.configure(menu=style)


root.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter menu ttk


    【解决方案1】:

    也许有更好的想法来实现它。我的想法是发送鼠标事件。

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    root.option_add("*Menu.borderWidth", "0")
    root.option_add("*Menu.activeBorderWidth", "0")
    root.option_add("*Menu.background", "black")
    
    
    style = ttk.Style(root)
    
    fr = ttk.Frame(root)
    
    btn_menu = ttk.Menubutton(fr, text='fegvd')
    btn_menu.grid(row=0, column=0)
    
    def func1(e):
        e.widget.event_generate("<Button-1>") # send a mouse press event
    
    btn_menu.bind("<Enter>",func1) # when your mouse enter this widget
    btn =ttk.Button(fr, text='grfbvgfev')
    btn.grid(row=0, column=1)
    
    btn_menu_st = ttk.Menubutton(fr, text='Gds')
    btn_menu_st.grid(row=0, column=2)
    
    fr.pack(fill='x')
    
    
    file = tk.Menu(btn_menu, tearoff=0, foreground='white')
    file.add_command(label='ГЫГ')
    
    style = tk.Menu(btn_menu_st, tearoff=0, foreground='white')
    style.add_command(label='Ugu')
    
    btn_menu.configure(menu=file)
    btn_menu_st.configure(menu=style)
    
    
    root.mainloop()
    

    我发现.post 是个不错的方法。

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    root.option_add("*Menu.borderWidth", "0")
    root.option_add("*Menu.activeBorderWidth", "0")
    root.option_add("*Menu.background", "black")
    
    
    style = ttk.Style(root)
    
    fr = ttk.Frame(root)
    
    btn_menu = ttk.Menubutton(fr, text='fegvd')
    btn_menu.grid(row=0, column=0)
    def func1(e):
        file.post(e.widget.winfo_rootx(),e.widget.winfo_rooty()+e.widget.winfo_height())
    
    btn_menu.bind("<Enter>",func1)
    btn =ttk.Button(fr, text='grfbvgfev')
    btn.grid(row=0, column=1)
    
    btn_menu_st = ttk.Menubutton(fr, text='Gds')
    btn_menu_st.grid(row=0, column=2)
    
    fr.pack(fill='x')
    
    
    file = tk.Menu(btn_menu, tearoff=0, foreground='white')
    file.add_command(label='ГЫГ')
    
    style = tk.Menu(btn_menu_st, tearoff=0, foreground='white')
    style.add_command(label='Ugu')
    
    btn_menu.configure(menu=file)
    btn_menu_st.configure(menu=style)
    
    root.mainloop()
    

    但是.unpost无法在我的电脑上运行,我找到了this question

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 2020-12-20
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多