Search code examples
pythonuser-interfacefontspyqtqt-designer

How to Use Custom Fonts from Folder in PyQt6 Instead of Installed Fonts in Windows


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 Figure 1

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 First Program output is in Figure 2

But the font is not successfully applied in the GUI. Any solution?

Thank You

Figure 1

Figure 2

I have tried several codes but still the same result


Solution

  • Finally I found the solution as below:

        def set_information_font(self): # Method for 
        
        id = QFontDatabase.addApplicationFont("font/FRAMDCN.TTF")
        families = QFontDatabase.applicationFontFamilies(id)
        
        if self.info_item == 0:
            font = QFont(families[0], 18)  # Ganti "Arial" dan 12 sesuai keinginan
        elif self.info_item == 1:
            font = QFont(families[0], 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(families[0], 20))  # Atur font untuk label_connection
        self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
    

    I use QFontDataBase.