How can I use Keyboard Shortcuts in WxPython?

John Berden <[email protected]> Wed, 28 Sep 2022 13:36:56 -0700 (PDT)
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
------=_Part_2122_1373160329.1664397416583
Content-Type: multipart/alternative; 
	boundary="----=_Part_2123_1949608415.1664397416583"

------=_Part_2123_1949608415.1664397416583
Content-Type: text/plain; charset="UTF-8"

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()

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/0dcc70f9-d141-4085-a6cd-2431926195b4n%40googlegroups.com.

------=_Part_2123_1949608415.1664397416583
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,<br>Well, I have a simple text editor in WxPython.<br>So, I wish I could=
 call File/Open for CTRL+o, File/Save for CTRL+s, File/Save as for CTRL+Shi=
ft+s. And in addition, I need some other keys Keyboard Shortcuts.<br>Well, =
I've tried Keyboard Shortcuts (Accelerators), but it doesn't work.<br>Can s=
omeone help me please?<br><br>import os<br>import wx<br>class MainWindow(wx=
.Frame):<br>&nbsp; &nbsp; def __init__(self, parent, title):<br>&nbsp; &nbs=
p; &nbsp; &nbsp; wx.Frame.__init__(self, parent, title=3Dtitle, size=3D(800=
,600))<br>&nbsp; &nbsp; &nbsp; &nbsp; self.control =3D wx.TextCtrl(self, st=
yle=3Dwx.TE_MULTILINE)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.CreateStatusBar(=
)<br>&nbsp; &nbsp; &nbsp; &nbsp; filemenu =3D wx.Menu()<br>&nbsp; &nbsp; &n=
bsp; &nbsp; menuOpen =3D filemenu.Append(wx.ID_OPEN, "&amp;Open"," Open a f=
ile")<br>&nbsp; &nbsp; &nbsp; &nbsp; menuSave =3D filemenu.Append(wx.ID_SAV=
E, "&amp;Save"," Save a file")<br>&nbsp; &nbsp; &nbsp; &nbsp; menuSaveAs =
=3D filemenu.Append(wx.ID_SAVEAS, "&amp;Save as"," Save a file as")<br>&nbs=
p; &nbsp; &nbsp; &nbsp; menuAbout =3D filemenu.Append(wx.ID_ABOUT, "&amp;Ab=
out"," Information about this program")<br>&nbsp; &nbsp; &nbsp; &nbsp; menu=
Exit =3D filemenu.Append(wx.ID_EXIT,"E&amp;xit"," Terminate the program")<b=
r>&nbsp; &nbsp; &nbsp; &nbsp; menuBar =3D wx.MenuBar()<br>&nbsp; &nbsp; &nb=
sp; &nbsp; menuBar.Append(filemenu,"&amp;File")<br>&nbsp; &nbsp; &nbsp; &nb=
sp; self.SetMenuBar(menuBar)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.Bind(wx.EV=
T_MENU, self.OnOpen, menuOpen)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.Bind(wx.=
EVT_MENU, self.OnSave, menuSave)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.Bind(w=
x.EVT_MENU, self.OnSaveAs, menuSaveAs)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.=
Bind(wx.EVT_MENU, self.OnAbout, menuAbout)<br>&nbsp; &nbsp; &nbsp; &nbsp; s=
elf.Bind(wx.EVT_MENU, self.OnExit, menuExit)<br>&nbsp; &nbsp; &nbsp; &nbsp;=
 exit_id =3D wx.NewId()<br>&nbsp; &nbsp; &nbsp; &nbsp; self.Bind(wx.EVT_MEN=
U, self.OnKeyExit, id=3Dexit_id)<br>&nbsp; &nbsp; &nbsp; &nbsp; a_tbl =3D w=
x.AcceleratorTable([(wx.ACCEL_CTRL,&nbsp; ord('Q'), exit_id )])<br>&nbsp; &=
nbsp; &nbsp; &nbsp; self.SetAcceleratorTable(a_tbl)<br>&nbsp; &nbsp; &nbsp;=
 &nbsp; self.Show(True)<br>...<br>&nbsp; &nbsp; def onKeyExit(self, e):<br>=
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.Close()<br>app =3D wx.App(Fa=
lse)<br>frame =3D MainWindow(None, "Test")<br>app.MainLoop()<br>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;wxPython-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">wxpy=
[email protected]</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/wxpython-users/0dcc70f9-d141-4085-a6cd-2431926195b4n%40googlegro=
ups.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d=
/msgid/wxpython-users/0dcc70f9-d141-4085-a6cd-2431926195b4n%40googlegroups.=
com</a>.<br />

------=_Part_2123_1949608415.1664397416583--

------=_Part_2122_1373160329.1664397416583--