Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:28:17+00:00 2026-06-13T03:28:17+00:00

I am trying to programmatically create and bind a table of accelerators in wxPython

  • 0

I am trying to programmatically create and bind a table of accelerators in wxPython in a loop so that I don’t need to worry about getting and assigning new IDs to each accelerators (and with a view to inhaling the handler list from some external resource, rather than hard-coding them). I also pass in some arguments to the handler via a lambda since a lot of my handlers will be the same but with different parameters (move, zoom, etc).

The class is subclassed from wx.Frame and setup_accelerators() is called during initialisation.

def setup_accelerators(self):

    bindings = [
                  (wx.ACCEL_CTRL,  wx.WXK_UP, self.on_move, 'up'),
                  (wx.ACCEL_CTRL,  wx.WXK_DOWN, self.on_move, 'down'),
                  (wx.ACCEL_CTRL,  wx.WXK_LEFT, self.on_move, 'left'),
                  (wx.ACCEL_CTRL,  wx.WXK_RIGHT, self.on_move, 'right'),
                  ]


    accelEntries = []

    for binding in bindings:
        eventId = wx.NewId()
        accelEntries.append( (binding[0], binding[1], eventId) )

        self.Bind(wx.EVT_MENU, lambda event: binding[2](event, binding[3]), id=eventId)


    accelTable = wx.AcceleratorTable(accelEntries)
    self.SetAcceleratorTable(accelTable)

def on_move(self, e, direction):
    print direction

However, this appears to bind all the accelerators to the last entry, so that Ctrl+Up prints "right", as do all the other three. How to correctly bind multiple handlers in this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:28:18+00:00Added an answer on June 13, 2026 at 3:28 am

    Got it. the problem was you weren’t passing in your direction as an argument to your lambda (it was only looking at the event argument (and self) because that was all your lambda was defined as accepting) This page gave me the help I needed to get this working (here is a gist):

    import wx
    
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Programmatic binding of accelerators in wxPython", size=(450,150))
    
            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            bindings = [
                      (wx.ACCEL_CTRL,  wx.WXK_UP, 'up'),
                      (wx.ACCEL_CTRL,  wx.WXK_DOWN, 'down'),
                      (wx.ACCEL_CTRL,  wx.WXK_LEFT, 'left'),
                      (wx.ACCEL_CTRL,  wx.WXK_RIGHT, 'right'),
                      ]
    
    
            accelEntries = []
    
            for binding in bindings:
                eventId = wx.NewId()
                accelEntries.append( (binding[0], binding[1], eventId) )
    
                self.Bind(wx.EVT_MENU, lambda evt, temp=binding[2]: self.on_move(evt, temp), id=eventId)
    
            accelTable  = wx.AcceleratorTable(accelEntries)
            self.SetAcceleratorTable(accelTable )
         #----------------------------------------------------------------------
    
        def on_move(self, Event, direction):
            print "You pressed CTRL+"+direction
    
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

    My original answer (which is an acceptable alternate solution) is:

    import wx
    
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))
    
            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            bindings = [
                      (wx.ACCEL_CTRL,  wx.WXK_UP, self.on_move_up),
                      (wx.ACCEL_CTRL,  wx.WXK_DOWN, self.on_move_down),
                      (wx.ACCEL_CTRL,  wx.WXK_LEFT, self.on_move_left),
                      (wx.ACCEL_CTRL,  wx.WXK_RIGHT, self.on_move_right),
                      ]
    
    
            accelEntries = []
    
            for binding in bindings:
                eventId = wx.NewId()
                accelEntries.append( (binding[0], binding[1], eventId) )
    
                self.Bind(wx.EVT_MENU, binding[2], id=eventId)
    
            accelTable  = wx.AcceleratorTable(accelEntries)
            self.SetAcceleratorTable(accelTable )
         #----------------------------------------------------------------------
        def on_move_up(self, event):
            print "You pressed CTRL+up"
        def on_move_down(self, event):
            print "You pressed CTRL+down"
        def on_move_left(self, event):
            print "You pressed CTRL+left"
        def on_move_right(self, event):
            print "You pressed CTRL+right"
    
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a site programmatically in Sharepoint but I keep getting
I am trying to create programmatically a uitableview that can scroll and fit based
I'm trying to create a view programmatically. The result that i want to have
I'm trying to create configurable products programmatically in Magento 1.5.1. I understand I need
I'm trying to create an NSSearchField programmatically; however, I don't know how to find
Im trying to make my NSViews and windows that I create programmatically have a
I'm trying to programmatically create a window with my own custom OpenGL NSView as
I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when
I am trying to programmatically create a number of PDF documents with a watermark
I'm trying to create an xlsx file programmatically on iOS. Since the internal data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.