简体   繁体   中英

is it possible to have two font sizes on the same string (for a label or button)?

I'm creating a ui in maya using pyside2 (basically pyQt5) and I have a button on it that I'd like to have 2 separate font sizes for the text if that's possible....

I know I can adjust the font like this:

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)

But I'd like to make '10' have a larger font size and '(auto)' have a smaller font size if possible (rather than both being the same size). I just have no idea how to go about doing that... any help would be appreciated!

One trick is to place a QLabel on top of the QPushButton, and place HTML to set the different fonts.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM