如何修复“ ctrl +”的wxpython menuitem快捷方式,但标签应显示为“ Ctrl +逗号”

时间:2019-01-04 20:43:56

标签: wxpython keyboard-shortcuts

开发wxpython桌面应用程序,这需要一个菜单​​项,当我执行该应用程序并单击“ Ctrl +”时,该菜单项显示标签显示为“ SomeXXX \ tCtrl + Comma”。

wx.MenuItem(self.tMn,wx.ID_ANY,text =“ SomeXXX \ tCtrl + Comma”,kind = wx.ITEM_NORMAL,helpString =“ SomeXXX”)---此代码标签是正确的,但Ctrl +是不起作用。

wx.MenuItem(self.tMn,wx.ID_ANY,text =“ SomeXXX \ tCtrl +,”,kind = wx.ITEM_NORMAL,helpString =“ SomeXXX”)----这行代码Ctrl +,快捷方式有效,但menuitem文本也显示为“ SomeXXX Ctrl +”

如何将菜单项文本显示为“ SomeXXX Ctrl +逗号”,快捷方式应与“ Ctrl +”一起使用

wx.MenuItem(self.tMn,wx.ID_ANY,text =“ SomeXXX \ tCtrl + Comma”,kind = wx.ITEM_NORMAL,helpString =“ SomeXXX”)

1 个答案:

答案 0 :(得分:1)

您需要的是wx.AcceleratorTable。然后,您可以按自己的方式命名菜单项。这是一个示例(没有菜单):

import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Accelerator Tutorial", 
                          size=(500,500))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        randomId = wx.NewId()
        self.Bind(wx.EVT_MENU, self.onKeyCombo, id=randomId)
        accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord(','), 
                                          randomId )])
        self.SetAcceleratorTable(accel_tbl)

    def onKeyCombo(self, event):
        """"""
        print("You pressed CTRL+COMMA!")

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

这是该主题的几个链接:

更新-我进行了一些研究,找到了一个解决方案,只需菜单项即可。您可以调用 SetAccel 并传入加速器以使其起作用:

import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Accelerator Tutorial", 
                          size=(500,500))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        menubar = wx.MenuBar()
        file_menu = wx.Menu()
        my_id = wx.NewId()
        comma_item = file_menu.Append(my_id, 'Blah\tCtrl+Comma', 
                                      'The comma cmd')
        entry = wx.AcceleratorEntry(wx.ACCEL_CTRL, ord(','), my_id)
        self.Bind(wx.EVT_MENU, self.onKeyCombo, id=my_id)
        comma_item.SetAccel(entry)
        menubar.Append(file_menu, '&File')
        self.SetMenuBar(menubar)

    def onKeyCombo(self, event):
        """"""
        print("You pressed CTRL+COMMA!")

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()