QFont.setFamily()不使用自定义字体

2024-09-10 06:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为我的程序做一个漂亮的图形用户界面,我知道我也需要一个很酷的字体。所以我安装了'Ubuntu'字体和所有的权重。在

但当我尝试这个:

class MenuButton(QPushButton):
    def __init__(self, caption):
        super().__init__()
        self.setFixedHeight(60)
        self.setFixedWidth(100)
        self.setFlat(True)
        font = QFont()
        font.setFamily('Ubuntu')
        font.setWeight(QFont.Bold)
        font.setPixelSize(20)
        self.setFont(font)
        self.setStyleSheet("color: rgb(85,170,255);")
        self.setText(caption)

我找到了一种字体,但肯定不是我选的字体:

enter image description here

但是当我在我的应用程序开始时调用一个QFontDialog时,我可以看到Ubuntu字体,它被命名为Ubuntu!在

enter image description here

为什么它不起作用?我该怎么做才能让它起作用?在

感谢你的帮助。。。在

以下是:

^{pr2}$

Tags: self程序initubuntudef字体图形用户界面class
1条回答
网友
1楼 · 发布于 2024-09-10 06:21:49

为了解决您的问题,我们需要稍微修改一下您的代码。 首先在class MenuButton()下添加这行代码self.setFont(QtGui.QFont("Ubuntu", 20, QtGui.QFont.Bold))。在

记住要显示mainwindow,您可以通过添加此代码来实现这一点

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle(QtWidgets.QStyleFactory().create('windowsvista'))
    win = MainScreen()
    win.show()
    sys.exit(app.exec_())

启动后字体正确的窗口

enter image description here

代码

^{pr2}$

相关问题 更多 >