Today, learning the Font dialog box --QFontDialog () control.
QFontDialog () is inherited from a subclass QDialog () is used to select a given font (including font, size, style, etc.)
A. Constructor
QFontDialog () statement is very simple, you can directly call
f = QFontDialog ()
Another method is to specify the default font (Enabling Sample box displays the specified font)
font = QFont () font.setFamily ( ' Arial ' ) font.setPointSize(24) fd = QFontDialog(font,self)
Use II. Dialog
Here we borrow a button to start the Font dialog box, and then get the Font dialog box selection.
Here is knowledge: We are in the Open dialog box when using open () is the transfer function of a trough
QFontDialog.open(self, slot: PYQT_SLOT)
After the dialog is opened, a function of automatically connecting groove signals specified here fontSelected
Method 1: using open () function, binding SelectFont () function
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() def UI_test(self): BTN = the QPushButton ( ' Select Font ' , Self) are = QFont () font.setFamily ( ' Arial ' ) font.setPointSize(24) fd = QFontDialog(font,self) self.fd = fd btn.clicked.connect(lambda :fd.open(self.func)) pass DEF FUNC (Self, * args): Print ( ' font selection: ' ., self.fd.selectedFont () Family ()) # get the selected font IF __name__ == ' __main__ ' : app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
Scheme 2: exec using the return value, binding SelectFont () function
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() def UI_test(self): BTN = the QPushButton ( ' Select Font ' , Self) fd = QFontDialog(self) def font_select(): if fd.exec(): print(fd.selectedFont().family()) btn.clicked.connect(font_select) pass if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
Because there is a return value of exec, only click on the OK before returning 1, are canceled and closed returns 0. Only confirmed only get chosen font.
III. Setting Options
You can set buttons and other options through code
QFontDialog.setOption (Self, the Option: ' QFontDialog.FontDialogOption ' , ON: BOOL = ...) # FontDialogOption enumeration value NoButtons # does not display the Cancel button (useful for real-time dialog) DontUseNativeDialog # using Qt on Mac Apple's standard font instead of the original forest font ScalableFonts # display scalable fonts NonScalableFonts # display non-scalable fonts MonospacedFonts # display monospaced font ProportionalFonts # display proportional fonts
We can try the first option, that is, in a real-time dialog box to select a different font and then change the font in the label in real time
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.resize(800,500) self.UI_test() def UI_test(self): label = QLabel('字体',self) label.move(100,200) self.label = label BTN = the QPushButton ( ' Select Font ' , Self) are = QFont () font.setFamily ( ' Arial ' ) font.setPointSize(24) fd = QFontDialog(font,self) fd.setOption(QFontDialog.NoButtons) self.fd = fd btn.clicked.connect(lambda :fd.open(self.func)) pass fd.currentFontChanged.connect(self.label_font_change) DEF FUNC (Self, * args): Print ( ' font selection: ' ., self.fd.selectedFont () Family ()) # get the selected font DEF label_font_change (Self, font): self.label.setFont(font) self.label.adjustSize() if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
Of course, the above values may be enumerated by pipe '|' is connected simultaneously.
IV. Static method
QFontDialog.getFont () # -> tuple
We first define a button, the button connected to this slot function
def func(self): result = QFontDialog.getFont() print(result) pass
Each call will find slot function result is a tuple. Tuple contains a font and a Boolean object. Just click on the OK button boolean was true. Use this method to achieve a static adjust font
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.resize(800,500) self.UI_test() def UI_test(self): label = QLabel('字体',self) label.move(100,200) self.label = label BTN = the QPushButton ( ' Select Font ' , Self) btn.clicked.connect(self.func) def func(self): result = QFontDialog.getFont() if result[1]: self.label.setFont(result[0])
self.label.adjustSize() pass if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
() There are a number of high-level usage getFont
QFontDialog (the Font, parent, ' dialog title ' , font filter)
V. signal
There are two signals, one is the current font is changed
QFontDialog.currentFontChanged(self, font: QtGui.QFont)
This is simply select the font change sends a signal (commonly used in real-time dialog, there are cases above)
QFontDialog.fontSelected(self, font: QtGui.QFont)
The determination signal is sent when pressing the key.