字体的示例程序
QFontDialog.getFont()方法返回一个字体对象和一个标识,前者存储字体信息,后者标识是否点击了确定按钮。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QVBoxLayout, QWidget, QFontDialog
class MainWindow(QMainWindow):
textEdit = None
def __init__(self):
super().__init__()
self.textEdit = QTextEdit()
self.button = QPushButton("字体")
self.button.clicked.connect(self.func)
layout = QVBoxLayout()
widget = QWidget()
layout.addWidget(self.button)
layout.addWidget(self.textEdit)
widget.setLayout(layout)
self.setCentralWidget(widget)
self.setCentralWidget(widget)
def func(self):
font, ok = QFontDialog.getFont()
if ok:
self.textEdit.setFont(font)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())