wxPython:通过全局热键弹出菜单

时间:2019-05-10 14:02:52

标签: python wxpython

我有wxPython托盘应用程序的工作示例。 如何通过全局热键获取PopupMenu? 我借助键盘模块成功将on_left_down操作绑定到ctrl + shift + a。 当我在图标上单击鼠标左键时,它会起作用,但是当我按下热键时,只有打印动作。

Windows 7, Python 3.7.3

import wx.adv
import keyboard

TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'some_icon.png'

def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.Append(item)
    return item

class TaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self, frame):
        self.frame = frame
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)

    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Say Hello', self.on_hello)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu

    def set_icon(self, path):
        icon = wx.Icon(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)

    def on_left_down(self, event=None):
        print('Tray icon was left-clicked.')
        menu = self.CreatePopupMenu()
        print(self.PopupMenu(menu))

    def on_hello(self, event):
        print('Hello, world!')

    def on_exit(self, event):
        wx.CallAfter(self.Destroy)
        self.frame.Close()

class App(wx.App):
    def OnInit(self):
        frame=wx.Frame(None)
        self.SetTopWindow(frame)
        self.taskbar_icon = TaskBarIcon(frame)
        return True

def main():
    app = App(False)
    keyboard.add_hotkey('ctrl+shift+a',
                        app.taskbar_icon.on_left_down)
    app.MainLoop()


if __name__ == '__main__':
    main()

0 个答案:

没有答案