【发布时间】:2021-05-09 16:56:41
【问题描述】:
如何制作具有不同文本颜色的 QPushButton。例如,在我的情况下,我需要红色按钮文本的每个第一个字母,其余的将是蓝色,而且第一个字母的字体将比剩余的字母大一点(全部在 PyQt5 ,通过谷歌搜索,我会发现C++格式的代码,我无法将它转换成PyQt5)?
import sys, os
from PyQt5.QtWidgets import *
class Navigate_Between(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Mulit Color Text In Buttons")
self.dashboard = QPushButton()
self.dashboard.setText("DashBoard")
# text = QTextDocument()
# text.setHtml("<h2><i>Dash Board</i>""<font color=red>Qt!</font></h2>");
# pixmap = QPixmap()
# textimage = pixmap.scaled(self.width(),self.height())
# textimage.fill(Qt.transparent)
# painter = QPainter(textimage)
#
# icon = QIcon()
# self.dashboard.setIcon(icon)
# self.dashboard.setIconSize(textimage.rect().size())
self.file = QPushButton("File")
self.master = QPushButton("Master")
self.transcation = QPushButton("Transcation")
self.reports = QPushButton("Reports")
self.others = QPushButton("Others")
layout = QHBoxLayout()
layout.addWidget(self.dashboard)
layout.addWidget(self.file)
layout.addWidget(self.master)
layout.addWidget(self.transcation)
layout.addWidget(self.reports)
layout.addWidget(self.others)
self.setLayout(layout)
def main():
app = QApplication(sys.argv)
mainwindow = Navigate_Between()
mainwindow.show()
sys.exit(app.exec_())
if __name__ =="__main__":
main()
【问题讨论】: