Answer a question

Can't make Bold font for label. What is wrong with my code?

self.label = QtGui.QLabel('Bla', self)
self.label.setFont(QtGui.QFont.setBold(True))

Answers

setBold is a method of QFont: it needs an instance of QFont. You can't call directly QtGui.QFont.setBold(), because there is nothing to be set to bold.

You have to first create the QFont object, then set it to bold, then set it as the label's font.

myFont=QtGui.QFont()
myFont.setBold(True)
self.label.setFont(myFont)

Note that self.label.setFont(QtGui.QFont().setBold(True)) wouldn't work either, because setBold returns None.

If you'd like a one-liner, QFont can be created with arguments, and one of them is the weight. For a bold Times font:

self.label.setFont(QtGui.QFont("Times",weight=QtGui.QFont.Bold))
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐