wxPython中的快捷方式编辑器

问题描述 投票:0回答:1

我正在尝试制作一个带有热键的程序以进行复制和粘贴,我希望用户能够自定义按键。链接here提供了3种填充列表的方法,我认为使用快捷方式管理器的第三个选项会效果最好,但是当“快捷方式编辑器”对话框关闭时,我无法找到一种方法来检索用户输入的新热键。有人有办法吗?

python wxpython
1个回答
0
投票

我发现ShortcutEditor有点像猪,因为它失败了而没有告诉您原因,除非至少一个菜单项设置了位图。 ??(您需要提供自己的图像。)参见:

    editsc = wx.MenuItem(editmenu, wx.NewIdRef(), 'Edit Shortcuts')
    editsc.SetBitmap(wx.Bitmap('./myimage1.png'))
    editmenu.Append(editsc)

就是说,它确实触发了events,可以出于您的目的查询。这是一个简短的示例,可以在Edit Shortcuts菜单中找到Edit工具。

编辑快捷方式时,将显示并打印给定菜单项的旧快捷方式和新快捷方式。

import wx
import wx.lib.agw.shortcuteditor as SE

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 600))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar()

        # Setting up the menus
        filemenu = wx.Menu()
        editmenu = wx.Menu()
        infomenu = wx.Menu()
        # file menu
        filemenu.Append(wx.ID_OPEN, "Open\tCtrl+V")
        filemenu.Append(wx.ID_SAVE, "Save")
        filemenu.Append(wx.ID_SAVEAS, "Save as")
        filemenu.Append(wx.ID_EXIT, '&Quit', 'Quit application')
        # edit menu
        editmenu.Append(wx.ID_COPY, "Copy")
        editsc = wx.MenuItem(editmenu, wx.NewIdRef(), 'Edit Shortcuts')
        editsc.SetBitmap(wx.Bitmap('./myimage1.png'))
        editmenu.Append(editsc)
        editmenu.Append(wx.ID_CUT, "Cut")
        editmenu.Append(wx.ID_PASTE, "Paste")
        editmenu.AppendSeparator()
        editmenu.Append(wx.ID_UNDO, "Undo")
        editmenu.Append(wx.ID_REDO, "Re-do it")
        # info menu
        infomenu.Append(wx.ID_ABOUT, "About")

        # Creating the menubar.
        self.menubar = wx.MenuBar()
        # Add menus
        self.menubar.Append(filemenu, "&File")
        self.menubar.Append(editmenu, "&Edit")
        self.menubar.Append(infomenu, "&Help")
        # Adding the MenuBar to the Frame content.
        self.SetMenuBar(self.menubar)
        # bind file menu
        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
        self.Bind(wx.EVT_MENU, self.OnSc, id=editsc.GetId())
        self.Bind(wx.EVT_MENU, self.OnMenu, id=wx.ID_OPEN)

        self.Show(True)

    def OnMenu(self, event):
        print ("Menu item selected")

    #Edit shortcuts
    def OnSc(self, event):
        dlg = SE.ShortcutEditor(self)
        dlg.Bind(SE.EVT_SHORTCUT_CHANGED, self.SEchanges)
        dlg.FromMenuBar(self)

        if dlg.ShowModal() == wx.ID_OK:
            # Changes accepted, send back the new shortcuts to the wx.MenuBar
            dlg.ToMenuBar(self)

        dlg.Destroy()

    #Check which item changed via the shortcut event
    def SEchanges(self, event):
        sc = event.GetShortcut()
        # On my OS a continuation character follows Ctrl, Alt, Shift etc
        # meaning the definition has not finished yet
        if ord(sc.accelerator[-1]) > 300:
            return
        msg = "Id "+sc.label+" Changed from "+sc.originalAccelerator+" to "+sc.accelerator
        print(msg)
        wx.MessageBox(msg, 'Changes', wx.OK | wx.ICON_INFORMATION)

    def OnExit(self, event):
        self.Destroy()


app = wx.App(False)
frame = MainWindow(None, "Shortcut Editor")
app.SetTopWindow(frame)
app.MainLoop()

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.