PyQt5 字体对话框
PyQt5 带有一个你可能在文本编辑器中看到的字体对话框。
要打开此对话框,请导入 QFontDialog 调用:
QFontDialog.getFont()
![PyQt5 字体对话框](/img/Tutorial/PyQt5/PyQt5 Font Dialog.png)
PyQt5 字体对话框
下面的示例在按钮事件上打开字体对话框,所选字体(包括大小的字体系列)将作为字符串返回。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFontDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 font dialog - tastones.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('Open PyQt5 Font Dialog', self)
button.setToolTip('font dialog')
button.move(50,50)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
openFontDialog(self)
def openFontDialog(self):
font, ok = QFontDialog.getFont()
if ok:
print(font.toString())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())