Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在样式表中使用非标准自定义字体?_Python_Fonts_Pyqt_Pyqt4_Qtstylesheets - Fatal编程技术网

Python 如何在样式表中使用非标准自定义字体?

Python 如何在样式表中使用非标准自定义字体?,python,fonts,pyqt,pyqt4,qtstylesheets,Python,Fonts,Pyqt,Pyqt4,Qtstylesheets,我有一个PyQt4应用程序,它由一个外部.qss文件使用以下代码进行样式设置: ... app = QtGui.QApplication(sys.argv) stylesheet = open('mystylesheet.qss').read() app.setStyleSheet(stylesheet) ... 通常,我会在.qss文件中指定要使用的字体类型,如下所示: QMainWindow { font-family:arial; font-size:14px; } QLabel {

我有一个
PyQt4
应用程序,它由一个外部
.qss
文件使用以下代码进行样式设置:

...
app = QtGui.QApplication(sys.argv)
stylesheet = open('mystylesheet.qss').read()
app.setStyleSheet(stylesheet)
...
通常,我会在
.qss
文件中指定要使用的字体类型,如下所示:

QMainWindow
{
font-family:arial;
font-size:14px;
}
QLabel
{
font-family:Mf Wedding Bells;
font-size:16px;
}
但是,现在我想知道我是否可以指定从internet下载的自定义字体(例如,droidsansmone(True Type font)),而不是windows标准字体

注意:我使用的是32位WindowsXPSP3和Python2.7

更新1: 根据Ekhumoro的回答:

我可以使用下载的自定义字体,方法是在加载
样式表之前将其添加到字体数据库中:

QtGui.QFontDatabase.addApplicationFont("Resources/Mf Wedding Bells.ttf")
之后,我可以简单地使用我刚刚在样式表中添加的字体名称,如下所示:

QMainWindow
{
font-family:arial;
font-size:14px;
}
QLabel
{
font-family:Mf Wedding Bells;
font-size:16px;
}

而且很有效

这只是一个猜测,因为我自己无法测试它,但您可以在设置样式表之前尝试:

app = QtGui.QApplication(sys.argv)
QtGui.QFontDatabase.addApplicationFont('path/to/font')
# or load the font data directly
# QtGui.QFontDatabase.addApplicationFontFromData(fontdata)
stylesheet = open('mystylesheet.qss').read()
app.setStyleSheet(stylesheet)