Pyqt5 help

Alan Gauld alan.gauld at yahoo.co.uk
Sun Feb 2 09:52:25 EST 2020


On 01/02/2020 12:06, Souvik Dutta wrote:
> I was making a pyqt5 project and I ran into a problem. I want a button in
> one window to add a label in another window when clicked upon. But that is
> not happening.

What is happening? Don't expect us to run your buggy code!

>  Now the no. Of labels depend upon something, so I decided to
> use a for loop which is not working.

Looking at the code I see:


for a in self.n:
    NewLabel = QtWidgets.QLabel(self)
    NewLabel.setText(self.friend_name + "has birthday on " +
                     self.friend_date)
    NewLabel.setGeometry(QtCore.QRect(30, 250, 100, 100))

I'm no Qt expert but it seems that you are overwriting the same widget
(or widget location?) with all of the labels? And all of the labels are
identical since you use the same data to create them?


I would expect something more like (pseudo code!):

for a in self.n:
    newLabel = QtWidgets.QLabel(self)
    NewLabel.setText(self.friends[a].friend_name + "has birthday on " +
                     self.friends[a]._date)
    NewLabel.setGeometry(QtCore.QRect(self.calculateGeometry(a))
    self.friendLabels.append(newLabel)   # if you need a reference


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Python-list mailing list