PyQt基础_013_对话框类控件QFontDialog

QFontDialog

PyQt基础_013_对话框类控件QFontDialog_第1张图片

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class FontDialogDemo(QWidget):
    def __init__(self, parent=None):
        super(FontDialogDemo, self).__init__(parent)
        layout = QVBoxLayout()
        self.fontButton = QPushButton("choose font")
        self.fontButton .clicked.connect(self.getFont)
        layout.addWidget(self.fontButton )
        self.fontLineEdit = QLabel("Hello,测试字体例子")
        layout.addWidget(self.fontLineEdit )
        self.setLayout(layout)
        self.setWindowTitle("Font Dialog 例子")

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

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = FontDialogDemo()
    demo.show()
    sys.exit(app.exec_())

QFontCombox

PyQt基础_013_对话框类控件QFontDialog_第2张图片

import sys

from PyQt5.Qt import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFontComboBox")
        self.resize(500, 500)
        self.move(400, 250)
        self.setup_ui()

    def setup_ui(self):
        fcb = QFontComboBox(self)
        fcb.move(100, 100)
        label = QLabel(self)
        label.move(100, 200)
        label.setText("hello world")

        fcb.setFontFilters(QFontComboBox.MonospacedFonts) # 设置过滤器
        """
        QFontComboBox.AllFonts 显示所有字体
        QFontComboBox.ScalableFonts 显示可缩放字体
        QFontComboBox.MonospacedFonts 显示等宽字体
        QFontComboBox.ProportionalFonts 显示比例字体
        """
        fcb.setEditable(False) # 禁止用户编辑

        def set_font(font):
            label.setFont(font)
            label.adjustSize()

        fcb.currentFontChanged.connect(lambda font: set_font(font))

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

你可能感兴趣的:(PyQt,pyqt,python,开发语言)