【问题标题】:How to make a Multi color or two color with different font size QPush button?如何制作具有不同字体大小的 QPush 按钮的多色或两种颜色?
【发布时间】: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()

【问题讨论】:

    标签: python pyqt5


    【解决方案1】:

    您必须创建一个 QPixmap 来呈现 QTextDocument 的内容。

    class Navigate_Between(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("Mulit Color Text In Buttons")
    
            self.dashboard = QPushButton()
    
            document = QTextDocument()
            document.setDocumentMargin(0)
            document.setHtml("<h2><i>Dash Board</i> <font color=red>Qt!</font></h2>")
            
            pixmap = QPixmap(document.size().toSize())
            pixmap.fill(Qt.transparent)
            painter = QPainter(pixmap)
            document.drawContents(painter)
            painter.end()
    
            icon = QIcon(pixmap)
            self.dashboard.setIcon(icon)
            self.dashboard.setIconSize(pixmap.size())
    
            self.file = QPushButton("File")
            self.master = QPushButton("Master")
            self.transcation = QPushButton("Transcation")
            self.reports = QPushButton("Reports")
            self.others = QPushButton("Others")
    
            layout = QHBoxLayout(self)
            layout.addWidget(self.dashboard)
            layout.addWidget(self.file)
            layout.addWidget(self.master)
            layout.addWidget(self.transcation)
            layout.addWidget(self.reports)
            layout.addWidget(self.others)
    

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2012-02-05
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多