How to Use Custom Fonts from Folder in PyQt6 Instead of Installed Fonts in Windows
-
wrote on 31 Oct 2024, 04:24 last edited by
First I used this code to set the font on my GUI.
def set_information_font(self): # Method for if self.info_item == 0: font = QFont("/Franklin Gothic Medium Cond", 18) # Ganti "Arial" dan 12 sesuai keinginan elif self.info_item == 1: font = QFont("Franklin Gothic Medium Cond", 18) # Ganti "Arial" dan 12 sesuai keinginan print(f"Setting font for info_item = {self.info_item}") self.ui.label_information_2.setFont(font) # Atur font untuk label_information_2 self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);") self.ui.label_connection.setFont(QFont("Franklin Gothic Medium Cond", 20)) # Atur font untuk label_connection self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
So when I use this code, I use the font that has been installed in Windows.
The GUI of the First Program output is in the below
Now I want to use the font but the font source is in my file directory which is in the font folder. I tried with the code below:
def set_information_font(self): # Method for setting font font_path = os.path.join(os.path.dirname(__file__), "font/FRAMDCN.TTF") # Cek apakah font ada if not os.path.exists(font_path): print("Font tidak ditemukan:", font_path) return # Atur font untuk label_information_2 dan label_connection menggunakan path font if self.info_item == 0: font = QFont(font_path, 18) # Menggunakan path langsung ke file font elif self.info_item == 1: font = QFont(font_path, 18) # Menggunakan path langsung ke file font self.ui.label_information_2.setFont(font) # Atur font untuk label_information_2 self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);") self.ui.label_connection.setFont(QFont(font_path, 20)) # Atur font untuk label_connection self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
The GUI of the Second Program output is in the below
But the font is not successfully applied in the GUI. Any solution?
Thank You
-
First I used this code to set the font on my GUI.
def set_information_font(self): # Method for if self.info_item == 0: font = QFont("/Franklin Gothic Medium Cond", 18) # Ganti "Arial" dan 12 sesuai keinginan elif self.info_item == 1: font = QFont("Franklin Gothic Medium Cond", 18) # Ganti "Arial" dan 12 sesuai keinginan print(f"Setting font for info_item = {self.info_item}") self.ui.label_information_2.setFont(font) # Atur font untuk label_information_2 self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);") self.ui.label_connection.setFont(QFont("Franklin Gothic Medium Cond", 20)) # Atur font untuk label_connection self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
So when I use this code, I use the font that has been installed in Windows.
The GUI of the First Program output is in the below
Now I want to use the font but the font source is in my file directory which is in the font folder. I tried with the code below:
def set_information_font(self): # Method for setting font font_path = os.path.join(os.path.dirname(__file__), "font/FRAMDCN.TTF") # Cek apakah font ada if not os.path.exists(font_path): print("Font tidak ditemukan:", font_path) return # Atur font untuk label_information_2 dan label_connection menggunakan path font if self.info_item == 0: font = QFont(font_path, 18) # Menggunakan path langsung ke file font elif self.info_item == 1: font = QFont(font_path, 18) # Menggunakan path langsung ke file font self.ui.label_information_2.setFont(font) # Atur font untuk label_information_2 self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);") self.ui.label_connection.setFont(QFont(font_path, 20)) # Atur font untuk label_connection self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
The GUI of the Second Program output is in the below
But the font is not successfully applied in the GUI. Any solution?
Thank You
@KevinAR said in How to Use Custom Fonts from Folder in PyQt6 Instead of Installed Fonts in Windows:
But the font is not successfully applied in the GUI.
But both screenshots look different?
I also do not see any QFont constructor which takes path to a font file as first parameter.
I think you have to use https://doc.qt.io/qt-6/qfontdatabase.html#addApplicationFont
1/2