wxPython按钮快捷键如何使用'&spam

1 投票
1 回答
1504 浏览
提问于 2025-04-18 02:37

我应该怎么添加一个按钮的快捷键呢?

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

在我的平台上似乎不太好使。

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

找到的文章:

示例代码:

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

结果是:

S不是应该被下划线标记吗?

更新:

我其实直到刚才才试了这个热键,因为我之前没太好奇。

好吧,在这个例子中按下 alt+热键 对于 &spam... 是有效的!

但是:

热键的字母一开始并没有下划线。

只有在按下 alt 键时,字母才会出现下划线。 :(

1 个回答

0

正如@RobinDunn所建议的,这其实是GTK的设置问题。

简单来说,就是把 gtk-auto-mnemonics 的值从0改成1。

因为我使用的是XFCE4,所以我在GTK2中为特定主题启用了助记符,方法如下:

在这里输入图片描述

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

虽然这些功能可以用,但按下 alt 键后,能提前知道哪些“丑陋”的按钮有快捷键,而不需要额外按其他按钮,这样会更方便。

或者在GTK3中,把 gtk-auto-mnemonics 的值从0改成1,具体步骤如下:

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

结果是你不需要按alt键就能看到:

在这里输入图片描述

撰写回答