accelerator table and class functions?

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?

···

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@gmail.com; http://www.facebook.com/mehgcap

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

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)

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.

···

On 5/23/10 2:06 PM, Alex Hall wrote:

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?

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

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

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.
  

I think this is what you want:

import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        _id = wx.NewId()
        hotkeyList = [(wx.ACCEL_CTRL, wx.WXK_RETURN, _id)]
        table = wx.AcceleratorTable(hotkeyList)
        self.SetAcceleratorTable(table)
        self.Bind(wx.EVT_MENU, self.do_something, id=_id) # no menubar/menuitem!

    def do_something(self, event):
        wx.MessageBox("Hi", "Hello there :)")

app = wx.App(redirect=False)
f = TestFrame()
f.Show()
app.MainLoop()

···

--
Steven Sproat, BSc
http://www.whyteboard.org/

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

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.

I think this is what you want:

import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        _id = wx.NewId()
        hotkeyList = [(wx.ACCEL_CTRL, wx.WXK_RETURN, _id)]

I am likely missing an elementary concept here, but how does this work
since _id is a class var, not tied to the specific function you want
to call? I thought the int had to represent the function to be called,
not that function's class? Thanks.

···

On 5/24/10, Steven Sproat <sproaty@gmail.com> wrote:

        table = wx.AcceleratorTable(hotkeyList)
        self.SetAcceleratorTable(table)
        self.Bind(wx.EVT_MENU, self.do_something, id=_id) # no
menubar/menuitem!

    def do_something(self, event):
        wx.MessageBox("Hi", "Hello there :)")

app = wx.App(redirect=False)
f = TestFrame()
f.Show()
app.MainLoop()

--
Steven Sproat, BSc
http://www.whyteboard.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

import wx

class TestFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None)
         _id = wx.NewId()
         hotkeyList = [(wx.ACCEL_CTRL, wx.WXK_RETURN, _id)]

I am likely missing an elementary concept here, but how does this work
since _id is a class var,

Actually, it's just a local variable.

  not tied to the specific function you want

to call? I thought the int had to represent the function to be called,
not that function's class? Thanks.

         table = wx.AcceleratorTable(hotkeyList)
         self.SetAcceleratorTable(table)
         self.Bind(wx.EVT_MENU, self.do_something, id=_id)

The association between the id and the function is done in the Bind()

···

On 5/24/10 3:21 PM, Alex Hall wrote:

     def do_something(self, event):
         wx.MessageBox("Hi", "Hello there :)")

app = wx.App(redirect=False)
f = TestFrame()
f.Show()
app.MainLoop()

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

import wx

class TestFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None)
         _id = wx.NewId()
         hotkeyList = [(wx.ACCEL_CTRL, wx.WXK_RETURN, _id)]

I am likely missing an elementary concept here, but how does this work
since _id is a class var,

Actually, it's just a local variable.

Right, I soon saw that mistake. I was, for some reason, thinking self._id.

  not tied to the specific function you want

to call? I thought the int had to represent the function to be called,
not that function's class? Thanks.

         table = wx.AcceleratorTable(hotkeyList)
         self.SetAcceleratorTable(table)
         self.Bind(wx.EVT_MENU, self.do_something, id=_id)

The association between the id and the function is done in the Bind()

That makes sense, and I now have my table working. Now, for my own
curiosity, I wonder how it is that binding to a evt_menu event
triggers a function that is in a class with a frame with no suggestion
of a menu? I figure what is going on is that the binding ties the id
and function together with a menu event, and the accelerator table
looks for menu events? Again, it is working so no big deal if I do not
fully understand the intricacies here, but it is always best to know
why your code works, not just that it does work.

···

On 5/24/10, Robin Dunn <robin@alldunn.com> wrote:

On 5/24/10 3:21 PM, Alex Hall wrote:

     def do_something(self, event):
         wx.MessageBox("Hi", "Hello there :)")

app = wx.App(redirect=False)
f = TestFrame()
f.Show()
app.MainLoop()

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

Almost, semantically it's slightly different than that: When an accelerator key is pressed a menu event is generated by wx and sent, so bindings for wx.EVT_MENU will match and the associated handler will be called.

···

On 5/24/10 4:08 PM, Alex Hall wrote:

The association between the id and the function is done in the Bind()

That makes sense, and I now have my table working. Now, for my own
curiosity, I wonder how it is that binding to a evt_menu event
triggers a function that is in a class with a frame with no suggestion
of a menu? I figure what is going on is that the binding ties the id
and function together with a menu event, and the accelerator table
looks for menu events?

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

The association between the id and the function is done in the Bind()

That makes sense, and I now have my table working. Now, for my own
curiosity, I wonder how it is that binding to a evt_menu event
triggers a function that is in a class with a frame with no suggestion
of a menu? I figure what is going on is that the binding ties the id
and function together with a menu event, and the accelerator table
looks for menu events?

Almost, semantically it's slightly different than that: When an
accelerator key is pressed a menu event is generated by wx and sent, so
bindings for wx.EVT_MENU will match and the associated handler will be
called.

Makes sense. It is not too intuitive, at least to a relative newbie
like myself, but it does make sense when spelled out like that. To see
code linking a number to a keystroke makes sense, then to see that
number bound to a function makes sense, but when you tell it to bind
with a menu event it sort of breaks down. I am sure it is all in the
AcceleratorTable documentation and just takes some putting together
for the experienced wx programmer. Anyway, it makes sense now. Thanks!

···

On 5/24/10, Robin Dunn <robin@alldunn.com> wrote:

On 5/24/10 4:08 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