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> def __init__(self, parent, title):<br> &nbs=
p; wx.Frame.__init__(self, parent, title=3Dtitle, size=3D(800=
,600))<br> self.control =3D wx.TextCtrl(self, st=
yle=3Dwx.TE_MULTILINE)<br> self.CreateStatusBar(=
)<br> filemenu =3D wx.Menu()<br> &n=
bsp; menuOpen =3D filemenu.Append(wx.ID_OPEN, "&Open"," Open a f=
ile")<br> menuSave =3D filemenu.Append(wx.ID_SAV=
E, "&Save"," Save a file")<br> menuSaveAs =
=3D filemenu.Append(wx.ID_SAVEAS, "&Save as"," Save a file as")<br>&nbs=
p; menuAbout =3D filemenu.Append(wx.ID_ABOUT, "&Ab=
out"," Information about this program")<br> menu=
Exit =3D filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")<b=
r> menuBar =3D wx.MenuBar()<br> &nb=
sp; menuBar.Append(filemenu,"&File")<br> &nb=
sp; self.SetMenuBar(menuBar)<br> self.Bind(wx.EV=
T_MENU, self.OnOpen, menuOpen)<br> self.Bind(wx.=
EVT_MENU, self.OnSave, menuSave)<br> self.Bind(w=
x.EVT_MENU, self.OnSaveAs, menuSaveAs)<br> self.=
Bind(wx.EVT_MENU, self.OnAbout, menuAbout)<br> s=
elf.Bind(wx.EVT_MENU, self.OnExit, menuExit)<br> =
exit_id =3D wx.NewId()<br> self.Bind(wx.EVT_MEN=
U, self.OnKeyExit, id=3Dexit_id)<br> a_tbl =3D w=
x.AcceleratorTable([(wx.ACCEL_CTRL, ord('Q'), exit_id )])<br> &=
nbsp; self.SetAcceleratorTable(a_tbl)<br> =
self.Show(True)<br>...<br> def onKeyExit(self, e):<br>=
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" 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--