How can I use Keyboard Shortcuts in WxPython?

21 views
Skip to first unread message

John Berden

unread,
Sep 28, 2022, 4:36:56 PM9/28/22
to wxPython-users
Hi,
Well, I have a simple text editor in WxPython.
So, I wish I could call File/Open for CTRL+o, File/Save for CTRL+s, File/Save as for CTRL+Shift+s. And in addition, I need some other keys Keyboard Shortcuts.
Well, I've tried Keyboard Shortcuts (Accelerators), but it doesn't work.
Can someone help me please?

import os
import wx
class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(800,600))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu = wx.Menu()
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file")
        menuSave = filemenu.Append(wx.ID_SAVE, "&Save"," Save a file")
        menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "&Save as"," Save a file as")
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
        self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
        self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        exit_id = wx.NewId()
        self.Bind(wx.EVT_MENU, self.OnKeyExit, id=exit_id)
        a_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord('Q'), exit_id )])
        self.SetAcceleratorTable(a_tbl)
        self.Show(True)
...
    def onKeyExit(self, e):
            self.Close()
app = wx.App(False)
frame = MainWindow(None, "Test")
app.MainLoop()
Reply all
Reply to author
Forward
0 new messages