- PyQt5 Tutorial
- PyQt5 - Home
- PyQt5 - Introduction
- PyQt5 - What’s New
- PyQt5 - Hello World
- PyQt5 - Major Classes
- PyQt5 - Using Qt Designer
- PyQt5 - Signals & Slots
- PyQt5 - Layout Management
- PyQt5 - Basic Widgets
- PyQt5 - QDialog Class
- PyQt5 - QMessageBox
- PyQt5 - Multiple Document Interface
- PyQt5 - Drag & Drop
- PyQt5 - Database Handling
- PyQt5 - Drawing API
- PyQt5 - BrushStyle Constants
- PyQt5 - QClipboard
- PyQt5 - QPixmap Class
- PyQt5 Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - Useful Resources
- PyQt5 - Discussion
PyQt5 - QFontDialog Widget
Another commonly used dialog, a font selector widget is the visual appearance of QDialog class. Result of this dialog is a Qfont object, which can be consumed by the parent window.
The class contains a static method getFont(). It displays the font selector dialog. setCurrentFont() method sets the default Font of the dialog.
Example
The following example has a button and a label. When the button is clicked, the font dialog pops up. The font chosen by the user (face, style and size) is applied to the text on the label.
The complete code is as follows −
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class fontdialogdemo(QWidget): def __init__(self, parent = None): super(fontdialogdemo, self).__init__(parent) layout = QVBoxLayout() self.btn = QPushButton("choose font") self.btn.clicked.connect(self.getfont) layout.addWidget(self.btn) self.le = QLabel("Hello") layout.addWidget(self.le) self.setLayout(layout) self.setWindowTitle("Font Dialog demo") def getfont(self): font, ok = QFontDialog.getFont() if ok: self.le.setFont(font) def main(): app = QApplication(sys.argv) ex = fontdialogdemo() ex.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
Output
The above code produces the following output −
pyqt_basic_widgets.htm
Advertisements