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

2024-10-03 06:29:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我显示带有此代码的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()

这是显示的

enter image description here

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

enter image description here

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


Tags: theno代码selfappifalt快捷键
2条回答

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

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

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

enter image description here

守则:

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>

相关问题 更多 >