【问题标题】:Change button font of QMessageBox in PyQt5, when using setStyleSheet?使用setStyleSheet时在PyQt5中更改QMessageBox的按钮字体?
【发布时间】:2020-07-22 13:28:44
【问题描述】:

考虑这个例子,大部分来自https://pythonbasics.org/pyqt-qmessagebox/

import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

defaultfont = QtGui.QFont('Arial', 8)

def window():
  app = QApplication(sys.argv)
  win = QWidget()
  button1 = QPushButton(win)
  button1.setText("Show dialog!")
  button1.move(50,50)
  button1.clicked.connect(showDialog)
  win.setWindowTitle("Click button")
  win.show()
  sys.exit(app.exec_())

def showDialog():
  msgBox = QMessageBox()
  msgBox.setStyleSheet("QLabel{min-width: 200px;}")
  msgBox.setFont(defaultfont)
  #msgBox.button(QMessageBox.Ok).setFont(defaultfont) # nowork, msgBox.button(QMessageBox.Ok) is None
  #print(msgBox.buttons()) # []
  #print(msgBox.findChildren(QtWidgets.QDialogButtonBox)) # [<PyQt5.QtWidgets.QDialogButtonBox object at 0x0000000005f950d0>]
  #print(msgBox.findChildren(QtWidgets.QDialogButtonBox)[0].buttons()) # []
  #print(msgBox.findChildren(QtWidgets.QDialogButtonBox)[0].standardButtons()) # <PyQt5.QtWidgets.QDialogButtonBox.StandardButtons object at 0x0000000005f60580>
  msgBox.setIcon(QMessageBox.Information)
  msgBox.setText("Message box pop up window")
  msgBox.setWindowTitle("QMessageBox Example")
  msgBox.buttonClicked.connect(msgButtonClick)

  returnValue = msgBox.exec_()
  if returnValue == QMessageBox.Ok:
    print('OK clicked')

def msgButtonClick(i):
  print("Button clicked is:",i.text())

if __name__ == '__main__':
  window()

如代码所示,我尝试应用msgBox.setFont(defaultfont) - 事实上,它确实改变了大部分消息的字体 - 但它确实不会改变按钮的字体,如果@ 987654328@在场;在这种情况下,这就是 Raspberry Pi 上的样子:

但是,如果您对msgBox.setStyleSheet("QLabel{min-width: 200px;}") 行进行注释,那么该字体也会应用于按钮:

那么,我怎样才能同时使用setStyleSheet 命令, 更改消息框的字体 - 对于文本 按钮? (我知道窗口标题栏字体 受操作系统控制,无法通过pyqt5更改)。

【问题讨论】:

    标签: python pyqt5


    【解决方案1】:

    如果你想改变 QLabels 的最小宽度,那么你可以使用 setMinimumWidth():

    def showDialog():
        msgBox = QMessageBox()
        msgBox.setFont(defaultfont)
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setText("Message box pop up window")
        msgBox.setWindowTitle("QMessageBox Example")
        msgBox.buttonClicked.connect(msgButtonClick)
    
        for label in msgBox.findChildren(QtWidgets.QLabel):
            label.setMinimumWidth(200)
    
        returnValue = msgBox.exec_()
        if returnValue == QMessageBox.Ok:
            print("OK clicked")
    

    另一种解决方案是访问按钮并设置字体,但这是在使用 show() 方法后创建的:

    def showDialog():
        msgBox = QMessageBox()
        msgBox.setFont(defaultfont)
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setText("Message box pop up window")
        msgBox.setWindowTitle("QMessageBox Example")
        msgBox.buttonClicked.connect(msgButtonClick)
    
        msgBox.setStyleSheet("QLabel{min-width: 200px;}")
    
        msgBox.show()
        msgBox.findChild(QtWidgets.QPushButton).setFont(defaultfont)
    
        returnValue = msgBox.exec_()
        if returnValue == QMessageBox.Ok:
            print("OK clicked")
    

    【讨论】:

    • 我已经注意到 QDialogBu​​ttonBox 的这种行为,我真的不明白这是一个错误还是故意的:看起来,当应用样式表时,它们不仅停止了调色板传播,而且还停止了字体.您对此有什么见解吗?
    • 非常感谢,setMinimumWidth() 非常适合我!只是为了记录,我想更改 QLabels 的最小宽度,因为我想调整消息框对话框的大小,并且我从stackoverflow.com/questions/37668820/… 撕下了那个 CSS - 完全忘记了我也可以尝试setMinimumWidth()!也很高兴知道在 show() 方法之后可以访问按钮 - 否则我很困惑为什么我无法访问它们。
    • 只想注意这一点:如果你有一个基于类的窗口(这个问题和答案没有),我注意到像msgBox = QMessageBox(self) 这样的实例化,其中self 是对窗口类的引用,也可以防止按钮获取setFont 设置,但msgBox = QMessageBox() 似乎工作正常,就像在这个问题/答案中一样。
    【解决方案2】:

    添加到您的代码中,这一行:

    msgBox.setStyleSheet("QPushButton {color:red; font-family: Arial; font-size:8px;}")
    

    msgBox 上的 Ok 按钮将变为红色,并且您的字体!经测试!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多