【问题标题】:Python get list of available fonts on systemPython获取系统上可用字体的列表
【发布时间】:2016-02-26 09:24:12
【问题描述】:

如何获取系统中所有可用的字体(Linux)?

我搜索并找到了这个:http://www.lemoda.net/pango/list-fonts/ 但它是在 C 中并且依赖于 pango。

如何在 Python 中做到这一点?

【问题讨论】:

标签: python linux fonts


【解决方案1】:

您可以尝试在 ubuntu 上使用 fc-list,然后将每一行以 ":" 分割,以获取 ubuntu http://manpages.ubuntu.com/manpages/hardy/man1/fc-list.1.html 上的所有字体

map(lambda x : x.split(":")[1],commands.getstatusoutput('fc-list')[1].split("\n"))

虽然某些字体名称可能包含 python 可能无法正确打印的符号。

【讨论】:

    【解决方案2】:

    使用 PyQt5.QtGui.QFontDatabasefamilies 方法获取不同的可用字体

    QFontDatabase().families()
    

    下面是一个查看不同字体的gui程序

    程序:

    import sys
    import textwrap
    
    from PyQt5.QtWidgets import *
    from PyQt5 import QtWidgets, QtGui, QtCore
    from PyQt5.QtWidgets import QTextEdit
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    
    
    class Window(QWidget):
    
        def __init__(self):
            super().__init__()
            self.title = "Available fonts"
            self.width = 500
            self.height = 500
            self.top = 10
            self.left = 10
            self.initUI()
            self.show()
    
        def initUI(self):
            self.setGeometry(self.left, self.top, self.width, self.height)
            self.setWindowTitle(self.title)
            self.layout = QVBoxLayout()
    
            self.comboBox = QComboBox(self)
            self.families = QFontDatabase().families()
            print(type(self.families))
            self.comboBox.addItems(self.families)
            self.comboBox.setEditable(True)
            # Dont Add the new values to combobox
            self.comboBox.setInsertPolicy(QComboBox.NoInsert)
            # Autocompleting
            self.comboBox.completer().setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
            song = ("""
                My Heart Will Go On\n
                by Celine Dion\n
                Every night in my dreams
                I see you, I feel you
                That is how I know you go on
                Far across the distance
                And spaces between us
                You have come to show you go on
                Near, far, wherever you are
                I believe that the heart does go on
                Once more, you open the door
                And you're here in my heart
                And my heart will go on and on
                Love can touch us one time
                And last for a lifetime
                And never let go 'til we're gone
                Love was when I loved you
                One true time I'd hold to
                In my life, we'll always go on
                Near, far, wherever you are
                I believe that the heart does go on
                Once more, you open the door
                And you're here in my heart
                And my heart will go on and on
                You're here, there's nothing I fear
                And I know that my heart will go on
                We'll stay forever this way
                You are safe in my heart and
                My heart will go on and """)
            # print(song)
            # when ever new item is selected from comboxbox call textChanged method
            self.comboBox.currentTextChanged.connect(self.textChanged)
            self.textEdit = QPlainTextEdit(song)
            self.layout.addWidget(self.comboBox)
            self.layout.addWidget(self.textEdit)
            # setting layout
            self.setLayout(self.layout)
    
        # Called when combo box current text is changed
        def textChanged(self):
            fontStr = self.comboBox.currentText()
            if fontStr in self.families:
                self.textEdit.setFont(QFont(fontStr))
    
    
    if __name__ == "__main__":
        App = QApplication(sys.argv)
        window = Window()
        sys.exit(App.exec())
    

    输出:

    【讨论】:

    • 感谢您提供完整的代码示例!组合框中的向上/向下箭头也可以使用,因此可以轻松地逐个快速浏览字体列表。我还将self.textEdit.setFont(QFont(fontStr)) 更改为self.textEdit.setFont(QFont(fontStr, 24)) 以增加字体大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多