这是一个工作示例,基于Lithy的回答:
import sys
from PySide2.QtCore import *
from PySide2.QtWidgets import *
table_text = '\
Name Flower\n\
------ -------\n\
Violet Yes\n\
Robert No\n\
Daisy Yes\n\
Anna No\n\
'
class Widget(QWidget):
def __init__(self, parent= None):
super(Widget, self).__init__()
warning_text = 'warning_text'
info_text = 'info_text'
pt = 'colour name'
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText(warning_text)
msg.setInformativeText(info_text)
msg.setDetailedText("{}".format(table_text))
msg.setTextInteractionFlags(Qt.TextSelectableByMouse)
# print all children and their children to find out which widget
# is the one that contains the detailed text
for child in msg.children():
print('child:{}'.format(child))
print(' {}'.format(child.children()))
pname = 'QMessageBox'
cname = 'QTextEdit'
msg.setStyleSheet(
"""{} {} {{ background-color: red; color: black; font-family: Courier; }}""".format(pname, cname))
msg.exec_()
if __name__ == '__main__':
app = QApplication(sys.argv)
Widget()