简体   繁体   中英

Change button font of QMessageBox in PyQt5, when using setStyleSheet?

Consider this example, ripped mostly from 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()

As the code shows, I tried applying msgBox.setFont(defaultfont) - and indeed, it does change the font of most of the message - but it does not change the font of buttons, if the line msgBox.setStyleSheet("QLabel{min-width: 200px;}") is present; this is how it looks like on Raspberry Pi in that case:

按钮字体坏

However, if you comment the line msgBox.setStyleSheet("QLabel{min-width: 200px;}") , then the font is applied also to the button:

按钮字体确定

So, how can I both use the setStyleSheet command, and change the font of the message box - for both texts and the button? (I am aware the window title bar font is under the control of the OS, and cannot be changed via pyqt5).

Add to your code, this line:

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

The button Ok on msgBox will change to red color, and your font! Tested!

If you want to change the minimum width of the QLabels then you can use 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")

Another solution is to access the button and set the font, but this is created after using the show() method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM