【问题标题】:is it possible to have two font sizes on the same string (for a label or button)?是否可以在同一个字符串上有两种字体大小(对于标签或按钮)?
【发布时间】:2021-10-05 20:42:48
【问题描述】:

我正在使用 pyside2(基本上是 pyQt5)在 Maya 中创建一个 ui,并且我有一个按钮,如果可能的话,我想为文本设置 2 种单独的字体大小......

我知道我可以这样调整字体:

import PySide2.QtGui as QtGui
import PySide2.QtWidgets as QtWidgets

value_font = QtGui.QFont('Arial', 18)
value_font.setBold(True)
update_button = QtWidgets.QPushButton('10\n(auto)')
update_button.setFixedSize(75, 75)
update_button.setFont(value_font)

但如果可能的话,我想让 '10' 有更大的字体大小,而 '(auto)' 有更小的字体大小(而不是两者的大小相同)。我只是不知道该怎么做...任何帮助将不胜感激!

【问题讨论】:

    标签: python maya pyside2


    【解决方案1】:

    一个技巧是在 QPushButton 顶部放置一个 QLabel,并放置 HTML 以设置不同的字体。

    from PySide2.QtCore import Qt
    from PySide2.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout
    
    
    def main():
        app = QApplication()
        # app.setStyle("fusion")
    
        button = QPushButton()
    
        lay = QVBoxLayout(button)
        # lay.setContentsMargins(0, 0, 0, 0)
        label = QLabel(
            """<div style="font-size:75px">10</div><div style="font-size:18px">(auto)</div>""",
            alignment=Qt.AlignCenter,
        )
        label.setAttribute(Qt.WA_TransparentForMouseEvents, True)
        lay.addWidget(label)
    
        button.clicked.connect(lambda: print("clicked"))
    
        button.show()
    
        app.exec_()
    
    
    if __name__ == "__main__":
        main()
    

    【讨论】:

    • 谢谢!虽然我的最终解决方案并没有完全按照您展示的方式结束,但它给了我足够的帮助,让我找到了一种方法来满足我的需求!我不知道我可以将标签按字面意思放在按钮上
    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多