Python Forum

Full Version: Customizing QMessageBox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

QMessageBox can be displayed with translated-to-french standard english labels. Is it possible to customize the labels to display specific phrases ?
And is it also possible to change the message box geometry, labels' font and color, and so on ?

Arbiel
You can use Stylesheets and html in QMessageBox

# -*- coding: utf-8 -*-
#################################################################################
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox, QPushButton)

#################################################################################

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.createStatusBar()
        
        self.btn = QPushButton("show MessageBox")
        self.btn.clicked.connect(self.showMessage)
        
        self.setCentralWidget(self.btn)

        self.setWindowTitle("MessageBox")

    def createStatusBar(self):
        self.statusBar().setStyleSheet("font-size: 8pt; color: #888a85;")
        self.statusBar().showMessage("Ready")

    def msgbox(self, title, message):
        msg = QMessageBox(1, title, message, QMessageBox.Ok)
        msg.setStyleSheet("QLabel {min-width: 300px; min-height: 200px;}")
        msg.exec()
        
    def showMessage(self):
        title = 'Information'
        message = '<p style="font-size:13pt; color: #4e9a06;">Hello World</p>\n \
                    <p style="font-size:10pt; color: #cc0000;">Hello World Again</p> \
                    <p style="font-size:10pt; color: #5c3566;">Hello World Again And Again</p>'
        self.msgbox(title, message)


if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())