Python 如何修改pyqt5 messagebox以永久显示按钮上的快捷键

Python 如何修改pyqt5 messagebox以永久显示按钮上的快捷键,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,当我显示带有此代码的messagebox时 def exitActionProcedure(self, ask): """ # Method: exitActionProcedure. # Description: The procedure for closing the application # and all the opened files. """ if ask is True

当我显示带有此代码的messagebox时

def exitActionProcedure(self, ask):
    """
    # Method: exitActionProcedure.
    # Description: The procedure for closing the application
    # and all the opened files.
    """
    if ask is True:
        # self.statusbar.showMessage('exitActionProcedure start')
        buttonReply = QMessageBox.question(
            None, 'Exit', "Quit Program?",
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if buttonReply == QMessageBox.Yes:
            app.quit()
    else:
        app.quit()
这是显示的

按alt键时,按钮中会显示下划线/快捷键 像这样


如何在不按alt键的情况下显示下划线/快捷键?

修复程序是对pyqt5 5.15.0的升级 感谢eyllanesc指出这一点。

你可以试试。在我的Windows计算机上,只有Fusion样式显示下划线

我从菜单中的表单…预览中获得了Qt Designer的样式名称。Linux或Mac可能有不同的可用样式

守则:

from PyQt5 import uic, QtCore, Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox,
                             QStyleFactory)

form_class = uic.loadUiType('qstyle.ui')[0]


class MainWindow(QMainWindow, form_class):
    def __init__(self, parent=None):
     
        super(MainWindow, self).__init__(parent=parent)
        
        self.setupUi(self)
        
        self.setWindowTitle("QMessageBox Style Test")
        
        verString = 'PyQt version: ' + Qt.PYQT_VERSION_STR + '\n'
        verString += 'Qt version: ' + QtCore.qVersion()
        self.lblVersion.setText(verString)
    
    @QtCore.pyqtSlot(int)
    def on_bgrpRadio_buttonClicked(self, button):
        global app
        
        # style name is lower case version of objectName with rdb removed
        style_name = self.bgrpRadio.checkedButton().objectName()
        style_name = style_name.replace('rdb', '').lower()
        print(style_name)
        
        app.setStyle(QStyleFactory.create(style_name))
        
    @QtCore.pyqtSlot()
    def on_btnMessageBox_clicked(self):
        buttonReply = QMessageBox.question(
            None, 'Exit', "Quit Program?",
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            
if __name__ == '__main__':   
    
    app = QApplication([])
    win = MainWindow()
    win.show()
    app.exec_()
qstyle.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>410</width>
    <height>340</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
       <string>QWindowsStyle</string>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0">
        <widget class="QRadioButton" name="rdbPlastique">
         <property name="text">
          <string>plastique</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="1" column="0">
        <widget class="QRadioButton" name="rdbGTK">
         <property name="text">
          <string>gtk</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="0" column="2">
        <widget class="QRadioButton" name="rdbMacintosh">
         <property name="text">
          <string>macintosh</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="0" column="1">
        <widget class="QRadioButton" name="rdbWindowsXP">
         <property name="text">
          <string>windowsxp</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="2" column="1">
        <widget class="QRadioButton" name="rdbWindows">
         <property name="text">
          <string>windows</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="1" column="1">
        <widget class="QRadioButton" name="rdbWindowsVista">
         <property name="text">
          <string>windowsvista</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="3" column="2">
        <widget class="QRadioButton" name="rdbFusion">
         <property name="text">
          <string>fusion</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="1" column="2">
        <widget class="QRadioButton" name="rdbCDE">
         <property name="text">
          <string>cde</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="2" column="0">
        <widget class="QRadioButton" name="rdbCleanLooks">
         <property name="text">
          <string>cleanlooks</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
       <item row="2" column="2">
        <widget class="QRadioButton" name="rdbMotif">
         <property name="text">
          <string>motif</string>
         </property>
         <attribute name="buttonGroup">
          <string notr="true">bgrpRadio</string>
         </attribute>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QComboBox" name="comboBox"/>
    </item>
    <item row="2" column="0">
     <widget class="QPushButton" name="btnMessageBox">
      <property name="text">
       <string>MessageBox</string>
      </property>
     </widget>
    </item>
    <item row="3" column="0">
     <widget class="QLabel" name="lblVersion">
      <property name="text">
       <string>TextLabel</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>410</width>
     <height>36</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
 <buttongroups>
  <buttongroup name="bgrpRadio"/>
 </buttongroups>
</ui>

主窗口
0
0
410
340
主窗口
QWindowsStyle
塑料瓶
bgrradio
gtk
bgrradio
麦金塔
bgrradio
windowsxp
bgrradio
窗户
bgrradio
windowsvista
bgrradio
融合
bgrradio
cde
bgrradio
干净的外表
bgrradio
动机
bgrradio
对话框
文本标签
0
0
410
36

在我的例子中,默认情况下,我会在Linux和PyQt5 5.15.0以及python 3中获得下划线,您的环境是什么?环境:PyQt5版本:5.14.1,python 3.8.2,ubuntu 20。04@eyllanesc:感谢您提供指向更新的pyqt5版本的指针。这立刻解决了我的问题。所以这是一个Qt错误