wxPython Button Shortcut Accelerator How to '&spam'

烈酒焚心 提交于 2020-01-05 12:08:24

问题


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:

  • http://wiki.wxpython.org/Using%20Multi-key%20Shortcuts%20
  • http://wxpython-users.1045709.n5.nabble.com/wxPython-Shortcuts-for-buttons-td2288314.html

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:

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. :(


回答1:


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:

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



来源:https://stackoverflow.com/questions/23049406/wxpython-button-shortcut-accelerator-how-to-spam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!