凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

20. PyQt6 QFontDialog

2023-10-17 python 112
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QFontDialog, QSizePolicy, \
    QVBoxLayout, \
    QLabel
from PyQt6.QtGui import QGuiApplication


class App(QWidget):

    def __init__(self):
        super().__init__()

        self.lbl = None
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 QFontDialog")

        vbox = QVBoxLayout()

        btn = QPushButton("选择字体", self)
        btn.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
        btn.move(20, 20)
        btn.clicked.connect(self.showDialog)

        vbox.addWidget(btn)

        self.lbl = QLabel("我的字体", self)
        self.lbl.move(130, 130)

        vbox.addWidget(self.lbl)
        self.setLayout(vbox)

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def showDialog(self):
        font, ok = QFontDialog.getFont()
        if ok:
            self.lbl.setFont(font)

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    cls_app = App()
    cls_app.show()
    sys.exit(app.exec())

image.png

文章评论

0条评论