フォント選択ダイアログを開く
以下のスクリプトを実行してボタンをクリックするとフォント選択ダイアログが開きます。from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton, QFontDialog from PyQt6.QtGui import QFont class Window(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("pyQt6 sample") self.button1 = QPushButton('Select Font') self.button1.clicked.connect(self.showDialog1) self.setCentralWidget(self.button1) def showDialog1(self): ok, font = QFontDialog.getFont(QFont(), self, "Select font") if __name__ == "__main__": app = QApplication([]) ex =Window() ex.show() app.exec()
WSL2での結果
Windows 11での結果
参考までにWindows11で実行した時の結果ものせておきます。WSL2で各種フォントを試してみる
結果
わかりやすいようにBoldにしています。一番上がデフォルトの「Sans Serif」になります。設定はこちらの通りです。
設定ファイル(「settings.yaml」)
font_1: type: QLabel width: 400 height: 100 alignment: center fontPoint: 28 fontBold: True text: Aa Bb Yy Zz あいう font_2: type: QLabel width: 400 height: 100 alignment: center fontFamily: Times fontPoint: 28 fontBold: True text: Aa Bb Yy Zz あいう font_3: type: QLabel width: 400 height: 100 alignment: center fontFamily: Noto Mono fontPoint: 28 fontBold: True text: Aa Bb Yy Zz あいう font_4: type: QLabel width: 400 height: 100 alignment: center fontFamily: Monospace fontPoint: 28 fontBold: True text: Aa Bb Yy Zz あいう font_5: type: QLabel width: 400 height: 100 alignment: center fontFamily: Nimbus Sans L fontPoint: 28 fontBold: True text: Aa Bb Yy Zz あいう font_6: type: QLabel width: 400 height: 100 alignment: center fontFamily: URW Gothic L fontPoint: 28 fontBold: True text: Aa Bb Yy Zz あいう
Pythonスクリプト
from PyQt6.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout from constructGUI import construct class Window(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("sample") self.label_1 = construct(QLabel(), 'settings.yaml', 'font_1') self.label_2 = construct(QLabel(), 'settings.yaml', 'font_2') self.label_3 = construct(QLabel(), 'settings.yaml', 'font_3') self.label_4 = construct(QLabel(), 'settings.yaml', 'font_4') self.label_5 = construct(QLabel(), 'settings.yaml', 'font_5') self.label_6 = construct(QLabel(), 'settings.yaml', 'font_6') layout = QVBoxLayout() layout.addWidget(self.label_1) layout.addWidget(self.label_2) layout.addWidget(self.label_3) layout.addWidget(self.label_4) layout.addWidget(self.label_5) layout.addWidget(self.label_6) self.setLayout(layout) if __name__ == "__main__": app = QApplication([]) ex =Window() ex.show() app.exec()
「constructGUI」についてはこちらを参照して下さい。
touch-sp.hatenablog.com
動作環境
Ubuntu 20.04 on WSL2
python 3.9.5
PyQt6==6.2.3 PyQt6-Qt6==6.2.4 PyQt6-sip==13.2.1 PyYAML==6.0