Hi again,
Sorry to keep sending emails, but I am completely stumped, even after
a bunch of Googling. How do I attach a function to a keypress using an
accelerator table? I keep getting the error that "an integer is
required", which makes sense, but I am at a loss as to how to get an
integer that will represent my function? As a very stripped-down
example:
class c():
def __init__(self, x):
self.x=x
def test(self):
print(self.x)
How do I attach, say, the enter key (wx.WXK_RETURN) to self.test() in
an accelerator table? More specifically, how do I get an integer that
will represent the test function so the table will be happy?
The Main.py module in the demo uses an accelerator table like this:
aTable = wx.AcceleratorTable([(wx.ACCEL_ALT, ord('X'),
exitItem.GetId()),
(wx.ACCEL_CTRL, ord('H'),
helpItem.GetId()),
(wx.ACCEL_CTRL, ord('F'),
findItem.GetId()),
(wx.ACCEL_NORMAL, wx.WXK_F3,
findNextItem.GetId()),
(wx.ACCEL_NORMAL, wx.WXK_F9,
shellItem.GetId()),
])
self.SetAcceleratorTable(aTable)
I had found a similar example online, but it was for menus, not class
functions. It had an example of tying to a function, but I could not
understand how the wx.NewId() linked to the function.
In this case the fooItem objects are menu items that are returned from
menu.Append() calls. That's because menu events are sent for
accelerator keys, and so we want these accelerators to be sent with the
same IDs as the menu items, so only one Bind() is needed for each of
them. If you want your accelerators to not have to also be in the menu
then you can create your own IDs with wx.NewId and then use those values
in the Bind() statements to connect event handlers to the accelerators.
Yes, I am trying to attach to functions; in fact, my window has no
menuBar object at all, and it will likely not have one anytime soon
(it does not need one). In my app, I tried both wx.NewId() as well as
Python's id(self.my_function), but neither worked.
self.hotkeyList=[(wx.ACCEL_CTRL, wx.WXK_RETURN, id(self.test))]
self.hotkeys=wx.AcceleratorTable(self.hotkeyList)
self.parentFrame.SetAcceleratorTable(self.hotkeys)
Pressing ctrl-enter in my app does nothing at all, not even causing an
exception. I have also tried this:
tid=wx.NewId()
self.parentFrame.Bind(wx.EVT_KEY_DOWN, self.test, id=tid)
and then put "tid" into my table instead of id(test).
As I see it, the problem is that I am not telling Python that
self.test() and the integer I am generating are one and the same, and
I do not know how to do this.
···
On 5/24/10, Robin Dunn <robin@alldunn.com> wrote:
On 5/23/10 2:06 PM, Alex Hall wrote:
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@gmail.com; Redirecting...
--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en