通过按钮打开字体选择对话框,将选择好的字体,赋值给Label标签,以此来改变字体的大小
""" 字体对话框 QFontDialog """ import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt class QFontDialogDemo(QWidget): def __init__(self): super(QFontDialogDemo, self).__init__() self.initUI() def initUI(self): self.setWindowTitle("Font Dialog例子") layout = QVBoxLayout() self.fontButton = QPushButton('选择字体') self.fontButton.clicked.connect(self.getFont) layout.addWidget(self.fontButton) self.fontLabel = QLabel("Hello, 测试字体例子") layout.addWidget(self.fontLabel) self.setLayout(layout) def getFont(self): #获得选择好的字体类型和大小 font, ok = QFontDialog.getFont() if ok: #将获得的字体大小赋值给标签, 进行字体的改变 self.fontLabel.setFont(font) if __name__ == "__main__": app = QApplication(sys.argv) main = QFontDialogDemo() main.show() sys.exit(app.exec_())
主界面
字体对话框(QFontDialog.getFont())