[英]Change button font of QMessageBox in PyQt5, when using setStyleSheet?
考慮這個例子,大部分來自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)
- 事實上,它確實改變了大部分消息的字體 - 但它不會改變按鈕的字體,如果行msgBox.setStyleSheet("QLabel{min-width: 200px;}")
存在; 在這種情況下,這就是 Raspberry Pi 上的樣子:
但是,如果您注釋行msgBox.setStyleSheet("QLabel{min-width: 200px;}")
,那么字體也會應用於按鈕:
那么,我如何既使用setStyleSheet
命令,又更改消息框的字體 - 文本和按鈕? (我知道 window 標題欄字體受操作系統控制,無法通過 pyqt5 更改)。
添加到您的代碼中,這一行:
msgBox.setStyleSheet("QPushButton {color:red; font-family: Arial; font-size:8px;}")
msgBox 上的按鈕 Ok 將變為紅色,並且您的字體! 經測試!
如果要更改 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.