下面是一个简单的Qt字体对话框实例:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFontDialog, QLabel
class FontDialogExample(QWidget):
def __init__(self):
super().__init__()
# 创建一个垂直布局
layout = QVBoxLayout(self)
# 创建一个标签,用于显示当前选中的字体信息
self.label = QLabel("Hello World!", self)
layout.addWidget(self.label)
# 创建一个按钮,点击后弹出字体对话框
button = QPushButton("Select Font", self)
button.clicked.connect(self.showFontDialog)
layout.addWidget(button)
def showFontDialog(self):
# 弹出字体对话框,并获取用户选择的字体
font, ok = QFontDialog.getFont()
# 如果用户选择了字体,则更新标签的字体信息
if ok:
self.label.setFont(font)
if __name__ == '__main__':
app = QApplication([])
window = FontDialogExample()
window.show()
app.exec_()
运行程序后,点击“Select Font”按钮即可弹出字体对话框,在对话框中选择好字体后点击“OK”按钮即可将所选字体应用到标签上。