Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bold font in Label with setBold method

Tags:

python

pyqt4

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))
like image 515
D.Sokolov Avatar asked Dec 21 '15 15:12

D.Sokolov


Video Answer


2 Answers

self.label.setStyleSheet("font-weight: bold")

easier I believe

like image 128
cily Avatar answered Sep 19 '22 15:09

cily


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))
like image 28
Mel Avatar answered Sep 17 '22 15:09

Mel