Python PyQt Qlabel Resize

26.8k views Asked by At

I'm having trouble here trying to set my Qlabel size to be bigger. Here is my code. I'm not sure what to do. I have tried lots...

def __init__(self, parent=None):
    super(UICreator, self).__init__(parent)
    self.Creator = QPushButton("YouTube", self)
    self.Creator.resize(100, 40)
    self.Creator.move(25, 50)
    self.CreatorB2 = QPushButton("Twitter", self)
    self.CreatorB2.resize(100, 40)
    self.CreatorB2.move(275, 50)
    self.CreatorL = QLabel("Created By:", self)
    self.CreatorL.resize(100, 100)
    self.CreatorL.move(20, 300)
3

There are 3 answers

13
Taufiq Rahman On BEST ANSWER

If you're using PyQt4 then make sure you imported:

from PyQt4 import QtCore

then add this line to set size of the label:

self.CreatorL = QLabel("Created By:", self)
self.CreatorL.setGeometry(QtCore.QRect(70, 80, 100, 100)) #(x, y, width, height)
0
NL23codes On

setGeometry works perfectly unless you're using a layout where the dimensions need to be something specific, for which I use setFixedSize This should help guarantee that your widget doesn't get unintentionally compressed or expanded due to a grid layout or something like that.

So it'd be something like this:

from PyQt5 import QtWidgets

my_label = QtWidgets.QLabel()
my_label.setText('My Label')
my_label.setFixedSize(50, 10)
0
Woody On

To Add to Achilles comment since I foukd this useful...

the values actually go (x, y, width, height)

if you want to do a relative change then something like this will work:

    self.CreatorL = QLabel("Created By:", self)
    self.CreatorL.setGeometry(QtCore.QRect(self.CreatorL.x()+50, self.CreatorL.y(), self.CreatorL.width(), self.CreatorL.height())) 

where this example will move the label 50 pixels to the right. Self.CreatorL can be replaced by the name of the label object.