[wxpython-users] accelerator newbie, help
Mike Driscoll
mdriscoll at co.marshall.ia.us
Thu Apr 24 08:34:07 PDT 2008
Phillip Watts wrote:
> Re the winxp event problem, it was suggested
> what I really want is an acceleratortable so I
> coded this simple test (within a gui app)
>
> def altx():
> print 'altx'
> def dof3():
> print 'dof3'
>
> aTable = wx.AcceleratorTable([(wx.ACCEL_ALT, ord('X'), altx),
> (wx.ACCEL_NORMAL, wx.WXK_F3, dof3),
> ])
> SetAcceleratorTable(aTable)
>
> AND GOT:
> File "./wintest.py", line 69, in <module>
> (wx.ACCEL_NORMAL, wx.WXK_F3, dof3),
> File "/usr/lib/python2.5/site-packages/wx-2.6-gtk2-unicode/wx/_core.py",
> line 7986, in __init__
> newobj = _core_.new_AcceleratorTable(*args, **kwargs)
> TypeError: an integer is required
>
> altx should be a cmdID. How do I code that to call the event handler?
>
> Does acceleratortable only work with menus? If so no good.
> this must work on linux, win, maybe mac.
> Thanks.
>
>
Hi,
AcceleratorTable does use a menu for some reason. However, you don't
need to display the menu. Here's some sample code:
<code>
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='Accelerator Test')
self.panel = wx.Panel(self, wx.ID_ANY)
# menubar code
menubar = wx.MenuBar()
fileMenu = wx.Menu()
save_menu_item = fileMenu.Append(-1, '&Save\tCtrl+S', 'Saves the
Data')
printDefault_menu_item = fileMenu.Append(-1, '&Print\tCtrl+P',
'Prints to Default Printer')
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
# bind events to menu items
self.Bind(wx.EVT_MENU, self.onSave, save_menu_item)
self.Bind(wx.EVT_MENU, self.onPrint, printDefault_menu_item)
# accelerator table code
accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('P'),
printDefault_menu_item.GetId()),
(wx.ACCEL_CTRL, ord('S'),
save_menu_item.GetId())
])
self.SetAcceleratorTable(accel_tbl)
# show the frame
self.Show()
def onPrint(self, event):
print 'in onPrint'
def onSave(self, event):
print 'in onSave'
if __name__ == '__main__':
app = wx.PySimpleApp()
frm = MyFrame()
app.MainLoop()
</code>
If you comment out the line "menubar.Append(fileMenu, '&File')", then my
menu will not show up, but the keyboard events that I've bound in my
AcceleratorTable are still able to be fired.
A little goofy, but it works!
Mike
More information about the wxpython-users
mailing list