Python hosting : Host, run, and code Python in the cloud!
PyQt5 comes with a font dialog that you may have seen in a text editor. This is the typical dialog where you can select font, font size, font style and so on. Of course the look and feel depends on the operating system.
To open this dialog import QFontDialog call:
To use it, import QFontDialog from QtWidgets like this:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFontDialog
Related course:
PyQt5 font dialog The example below opens a font dialog on a button event, the selected font (font family including size) will be returned as a string.
Font Dialog using PyQt5
Opening a font dialog is as simple as
def openFontDialog (self ): font, ok = QFontDialog.getFont() if ok: print (font.toString())
A full code example is shown below
import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFontDialogfrom PyQt5.QtGui import QIconfrom PyQt5.QtCore import pyqtSlotclass App (QWidget ): def __init__ (self ): super ().__init__() self.title = 'PyQt5 font dialog - pythonspot.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_())
If you are new to programming Python PyQt, I highly recommend this book.
Download PyQT5 Examples
Leave a Reply: