如何向菜单项添加工具提示?

时间:2019-03-23 18:01:11

标签: python tkinter menu tooltip

我正在尝试为菜单栏项(例如[Save])添加工具提示,但是我无法立即获得所需的菜单项。我可以完全添加此工具提示吗?我在python 2.7中使用Tkinter

def createMenu(self):
    menu = Menu(root)
    root.config(menu=menu)
    filemenu = Menu(menu, tearoff=0)
    menu.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="Save", command=self.openBlankPy)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=self.exitApp)

1 个答案:

答案 0 :(得分:0)

  

问题:向菜单项添加工具提示

我不知道bind到菜单项。

如果class MenuTooltip y位置在菜单项<Motion>内,则以下Pointer使用事件.yposition查找菜单项。


class MenuTooltip(tk.Menu):
    def __init__(self, parent):
        """
        :param parent: The parent of this Menu, either 'root' or 'Menubar'
         .tooltip == List of tuple (yposition, text)
         .tooltip_active == Index (0-based) of the active shown Tooltip
         Bind events <Leave>, <Motion>
        """
        super().__init__(parent, tearoff=0)
        self.tooltip = []
        self.tooltip_active = None

        self.bind('<Leave>', self.leave)
        self.bind('<Motion>', self.on_motion)

    def add_command(self, *cnf, **kwargs):
        tooltip = kwargs.get('tooltip')
        if tooltip:
            del kwargs['tooltip']
        super().add_command(*cnf, **kwargs)
        self.add_tooltip(len(self.tooltip), tooltip)

    def add_tooltip(self, index, tooltip):
        """
        :param index: Index (0-based) of the Menu Item 
        :param tooltip: Text to show as Tooltip
        :return: None
        """
        self.tooltip.append((self.yposition(index) + 2, tooltip))

    def on_motion(self, event):
        """
        Loop .tooltip to find matching Menu Item
        """
        for idx in range(len(self.tooltip) - 1, -1, -1):
            if event.y >= self.tooltip[idx][0]:
                self.show_tooltip(idx)
                break

    def leave(self, event):
        """
        On leave, destroy the Tooltip and reset .tooltip_active to None
        """
        if not self.tooltip_active is None:
            print('leave()'.format())
            # destroy(<tooltip_active>)
            self.tooltip_active = None

    def show_tooltip(self, idx):
        """
        Show the Tooltip if not already shown, destroy the active Tooltip
        :param idx: Index of the Tooltip to show
        :return: None 
        """
        if self.tooltip_active != idx:
            # destroy(<tooltip_active>)
            self.tooltip_active = idx
            print('{}'.format(self.tooltip[idx][1]))
  

用法

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        menu = MenuTooltip(self)
        menu.add_command(label='Help 1', tooltip='\tToolTip.Help 1')
        menu.add_command(label='Help 2', tooltip='\tToolTip.Help 2')
        self.menubar.add_cascade(label="Help", menu=menu)

if __name__ == "__main__":
    App().mainloop()

使用Python测试:3.5

  

注意:无法在python 2.7上进行测试,如果适用于2.7,请进行报告