Search code examples
pythonbuttonwxpythonkeyboard-shortcuts

wxPython Button Shortcut Accelerator How to '&spam'


How should I go about adding a button shortcut / accelerator?

self.newItemButton = wx.Button(self.mainPanel, label='Scan &New Item')

Doesn't seem to work on my platform.

Python 2.7.3 (default, Sep 26 2013, 20:08:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx, os
>>> wx.version();print(os.system('uname -a&&lsb_release -a'))
'2.8.12.1 (gtk2-unicode)'
Linux NE-522 3.2.0-53-lowlatency-pae #55-Ubuntu SMP PREEMPT Mon Aug 26 22:52:24 UTC 2013 i686 i686 i386 GNU/Linux
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.3 LTS
Release:    12.04
Codename:   precise

Articles Found:

Example Code:

import wx
class SpamButton(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title)
        self.mainPanel = wx.Panel(self)
        self.thisSpamButton()
        self.Show()

    def thisSpamButton(self):
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            #---------------------------------------------
            # So the letter 's' should be underlined right?       | Here it is
            #-----------------------------------------------------v
            self.newItemButton = wx.Button(self.mainPanel, label='&spam')
            self.sizer.Add(self.newItemButton, 0, wx.ALL, 2)
            self.mainPanel.Layout()

def main():
    app = wx.App(False)
    MainFrame = SpamButton(None, 'This Button Short Cut is weird')
    app.MainLoop()

if __name__ == '__main__':
    main()

Results in:

Shouldn't the S be underlined?

Update:

I didn't actually try the hot key, until just now, just because I wasn't curious enough.

Okay so when pressing alt+hot_key in this example for &spam... it WORKS!

However:

The hotkey letter isn't initially rendered underlined.

The underline only renders when alt is pressed. :(


Solution

  • As @RobinDunn suggested it was a GTK configuration.

    Basically change gtk-auto-mnemonics from 0 to 1.

    Since I was using XFCE4, I enabled mnemonics in GTK2 for a specific theme like this:

    enter image description here

    #xfconf-query -c xsettings -p /Net/ThemeName
    
    #grab the current style/theme name
    VAR1=$(gconftool-2 --get /desktop/gnome/interface/gtk_theme)
    # change to that theme directory
    cd /usr/share/themes/$VAR1/gtk-2.0
    # replace gtk-auto-mnemonics = 0 with 'gtk-auto-mnemonics=1# = 0'
    sudo sed -i 's/gtk-auto-mnemonics/gtk-auto-mnemonics=1#/g' gtkrc
    # change the theme to something else ... (Clearlooks, Ambiance, some_theme)
    xfconf-query -c xsettings -p /Net/ThemeName -s Default
    xfconf-query -c xsettings -p /Net/ThemeName -s $VAR1
    #gconftool-2 --type=string -s /desktop/gnome/interface/gtk_theme ????
    

    Although they work, by pressing alt its nice to know upfront without pressing extra buttons which of the 'ugly' buttons has accelerators.

    Or in GTK3 change gtk-auto-mnemonics from 0 to 1 in:

    /usr/share/themes/CURRENT_THEME_NAME/gtk-3.0/settings.ini
    

    Results without having to press alt:

    enter image description here