How to get the font name as readable text
-
After setting the font to a label, I would like to print it for info.
The setFont method works, but when printing the font info it shows:
Code:print(self.label.fontInfo, self.fontComboBox.currentFont)
<built-in method fontInfo of QLabel object at 0x10dd91c60>
How do I show the font name as text?
#pyqt6 - PyQt6_V2.0_Designer_08.10.2024.py import sys, os from PyQt6.QtWidgets import (QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel, QMessageBox, QMainWindow, QGraphicsView, QGraphicsScene) from PyQt6.QtGui import QPixmap from PyQt6.QtGui import QMovie, QFont, QFontInfo from PyQt6 import uic, QtGui from PyQt6.QtWidgets import QFontComboBox #from login_form_1 import Ui_Login_form from inspect import getmembers, isfunction class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.ui = uic.loadUi('Test_font_v1.0.ui', self) base_dir = os.path.dirname(__file__) print("Current folder: ",os.getcwd()) print("Path relative to: ", base_dir) self.label_2.setScaledContents(True) self.fontComboBox.currentFontChanged.connect(self.font_to_label) self.comboBox.currentTextChanged.connect(self.list_changed) def list_changed(self, s): self.label.setText(s) def font_to_label(self): #self.label.font = self.fontComboBox.currentFont() self.label.setFont(self.fontComboBox.currentFont()) self.comboBox.setFont(self.fontComboBox.currentFont()) print(self.label.fontInfo, self.fontComboBox.currentFont) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
I work with the designer. here is my ui file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>526</width> <height>401</height> </rect> </property> <property name="font"> <font> <family>Courier New</family> <bold>true</bold> </font> </property> <property name="focusPolicy"> <enum>Qt::ClickFocus</enum> </property> <property name="windowTitle"> <string>Bild</string> </property> <property name="layoutDirection"> <enum>Qt::LeftToRight</enum> </property> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>98</width> <height>36</height> </rect> </property> <property name="font"> <font> <family>Courier New</family> <pointsize>18</pointsize> <bold>true</bold> </font> </property> <property name="text"> <string>TextLabel</string> </property> <property name="textInteractionFlags"> <set>Qt::LinksAccessibleByMouse|Qt::TextEditable|Qt::TextEditorInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> </property> </widget> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>120</x> <y>10</y> <width>108</width> <height>29</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>30</x> <y>110</y> <width>461</width> <height>271</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string/> </property> <property name="pixmap"> <pixmap>IMG_0205.JPG</pixmap> </property> <property name="scaledContents"> <bool>true</bool> </property> </widget> <widget class="QFontComboBox" name="fontComboBox"> <property name="geometry"> <rect> <x>230</x> <y>10</y> <width>275</width> <height>32</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="font"> <font> <family>Arial</family> <pointsize>12</pointsize> <bold>false</bold> </font> </property> </widget> <widget class="QComboBox" name="comboBox"> <property name="geometry"> <rect> <x>90</x> <y>50</y> <width>103</width> <height>32</height> </rect> </property> <property name="editable"> <bool>true</bool> </property> <property name="currentText"> <string>Eins, zwei, drei</string> </property> <property name="maxCount"> <number>12</number> </property> <property name="insertPolicy"> <enum>QComboBox::InsertAlphabetically</enum> </property> <property name="placeholderText"> <string>Hallo</string> </property> </widget> <action name="actionWrong_PWD"> <property name="text"> <string>Wrong_PWD</string> </property> </action> </widget> <resources/> <connections/> </ui>
-
Thank you JonB and SGaist,
found it thanks to your tips:
font = self.label.font() print(QFont.family(font))
The same works with QFontInfo:
font = self.label.fontInfo() print(QFontInfo.family(font))
Then it gives out the font as text (str).
This works also:
print(QFont.family(self.label.font()))
Instead of print you can put in a str variable:
font = QFont.family(self.label.font()) print(type(font))
Returns
<class 'str'>@EarlyBird
I assume this is code copied from an actual working example? Because you use:QFont.family(font) QFontInfo.family(font)
If you look at these methods in PySide documentation you find QFont.family() and QFontInfo.family(). These are member methods of
QFont
/QFontInfo
, andfamily()
does not accept a parameter. You can call them on aQFont
orQFontInfo
instance. I can guess that Python may allow you to invoke it via the class method with a parameter of aQFont
instance as a parameter, but this strikes me as "unusual" if you choose to call your instance methods in this way.I would suggest the "standard" way to call it would be
print(self.label.font().family()) // or if you really want print(self.label.fontInfo().family())
[I should not have written
familyName()
in my previous reply.]Separately, if you are interested there is also
print(self.label.font().toString())
which would return a whole list of attributes of the font.
-
After setting the font to a label, I would like to print it for info.
The setFont method works, but when printing the font info it shows:
Code:print(self.label.fontInfo, self.fontComboBox.currentFont)
<built-in method fontInfo of QLabel object at 0x10dd91c60>
How do I show the font name as text?
#pyqt6 - PyQt6_V2.0_Designer_08.10.2024.py import sys, os from PyQt6.QtWidgets import (QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout, QLabel, QMessageBox, QMainWindow, QGraphicsView, QGraphicsScene) from PyQt6.QtGui import QPixmap from PyQt6.QtGui import QMovie, QFont, QFontInfo from PyQt6 import uic, QtGui from PyQt6.QtWidgets import QFontComboBox #from login_form_1 import Ui_Login_form from inspect import getmembers, isfunction class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.ui = uic.loadUi('Test_font_v1.0.ui', self) base_dir = os.path.dirname(__file__) print("Current folder: ",os.getcwd()) print("Path relative to: ", base_dir) self.label_2.setScaledContents(True) self.fontComboBox.currentFontChanged.connect(self.font_to_label) self.comboBox.currentTextChanged.connect(self.list_changed) def list_changed(self, s): self.label.setText(s) def font_to_label(self): #self.label.font = self.fontComboBox.currentFont() self.label.setFont(self.fontComboBox.currentFont()) self.comboBox.setFont(self.fontComboBox.currentFont()) print(self.label.fontInfo, self.fontComboBox.currentFont) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
I work with the designer. here is my ui file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>526</width> <height>401</height> </rect> </property> <property name="font"> <font> <family>Courier New</family> <bold>true</bold> </font> </property> <property name="focusPolicy"> <enum>Qt::ClickFocus</enum> </property> <property name="windowTitle"> <string>Bild</string> </property> <property name="layoutDirection"> <enum>Qt::LeftToRight</enum> </property> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>98</width> <height>36</height> </rect> </property> <property name="font"> <font> <family>Courier New</family> <pointsize>18</pointsize> <bold>true</bold> </font> </property> <property name="text"> <string>TextLabel</string> </property> <property name="textInteractionFlags"> <set>Qt::LinksAccessibleByMouse|Qt::TextEditable|Qt::TextEditorInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> </property> </widget> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>120</x> <y>10</y> <width>108</width> <height>29</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>30</x> <y>110</y> <width>461</width> <height>271</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string/> </property> <property name="pixmap"> <pixmap>IMG_0205.JPG</pixmap> </property> <property name="scaledContents"> <bool>true</bool> </property> </widget> <widget class="QFontComboBox" name="fontComboBox"> <property name="geometry"> <rect> <x>230</x> <y>10</y> <width>275</width> <height>32</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="font"> <font> <family>Arial</family> <pointsize>12</pointsize> <bold>false</bold> </font> </property> </widget> <widget class="QComboBox" name="comboBox"> <property name="geometry"> <rect> <x>90</x> <y>50</y> <width>103</width> <height>32</height> </rect> </property> <property name="editable"> <bool>true</bool> </property> <property name="currentText"> <string>Eins, zwei, drei</string> </property> <property name="maxCount"> <number>12</number> </property> <property name="insertPolicy"> <enum>QComboBox::InsertAlphabetically</enum> </property> <property name="placeholderText"> <string>Hallo</string> </property> </widget> <action name="actionWrong_PWD"> <property name="text"> <string>Wrong_PWD</string> </property> </action> </widget> <resources/> <connections/> </ui>
@EarlyBird said in How to get the font name as readable text:
How do I show the font name as text?
I think the closest to a name for the font is the
famliyName()
. Or there issubstitutes()
if it can't be found. Don't know if it tells you if it has to use one. -
Hi,
As the error message suggests: you are trying to print the function themselves rather than what they return. You are missing parenthesis at the end of both calls.
-
Thank you JonB and SGaist,
found it thanks to your tips:
font = self.label.font() print(QFont.family(font))
The same works with QFontInfo:
font = self.label.fontInfo() print(QFontInfo.family(font))
Then it gives out the font as text (str).
This works also:
print(QFont.family(self.label.font()))
Instead of print you can put in a str variable:
font = QFont.family(self.label.font()) print(type(font))
Returns
<class 'str'> -
Thank you JonB and SGaist,
found it thanks to your tips:
font = self.label.font() print(QFont.family(font))
The same works with QFontInfo:
font = self.label.fontInfo() print(QFontInfo.family(font))
Then it gives out the font as text (str).
This works also:
print(QFont.family(self.label.font()))
Instead of print you can put in a str variable:
font = QFont.family(self.label.font()) print(type(font))
Returns
<class 'str'>@EarlyBird
I assume this is code copied from an actual working example? Because you use:QFont.family(font) QFontInfo.family(font)
If you look at these methods in PySide documentation you find QFont.family() and QFontInfo.family(). These are member methods of
QFont
/QFontInfo
, andfamily()
does not accept a parameter. You can call them on aQFont
orQFontInfo
instance. I can guess that Python may allow you to invoke it via the class method with a parameter of aQFont
instance as a parameter, but this strikes me as "unusual" if you choose to call your instance methods in this way.I would suggest the "standard" way to call it would be
print(self.label.font().family()) // or if you really want print(self.label.fontInfo().family())
[I should not have written
familyName()
in my previous reply.]Separately, if you are interested there is also
print(self.label.font().toString())
which would return a whole list of attributes of the font.
-