2

I'm trying to write a simple program that will display pc drives information like letter assigned, total memory in GB etc. Since everyone has a different number of drives this must be dynamic. I am using PyQt5 for GUI. I am trying to display letters of my drives but I can't seem to be able to dynamically add new widgets, I always get the last letter only. Here's how it looks like now. First I setup a grid:

self.gridLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget_3.setGeometry(QtCore.QRect(10, 210, 741, 71))
self.gridLayoutWidget_3.setObjectName("gridLayoutWidget_3")
self.DrivesData = QtWidgets.QGridLayout(self.gridLayoutWidget_3)
self.DrivesData.setContentsMargins(0, 0, 0, 0)
self.DrivesData.setObjectName("DrivesData")

Then I try to add new label widgets depending on the number of drives:

for disk in disksInfo:
    self.DRIVELETTERSPACE = QtWidgets.QLabel(self.gridLayoutWidget_3)
    self.DRIVELETTERSPACE.setObjectName("DRIVELETTERSPACE")
    self.DrivesData.addWidget(self.DRIVELETTERSPACE, 1, 0, 1, 1)

With the above code all I get displayed is the last drive's letter(E:). I think I understand that I shouldn't name all of them DRIVELETTERSPACE but then how can I make the names dynamic as well? Also, is this how I can dynamically add widgets in pyqt5? Or should I make the grid dynamic as well? Thanks.

7
  • 1
    try with key = 0 for disk in disksInfo: DRIVELETTERSPACE = QtWidgets.QLabel(self.gridLayoutWidget_3) DRIVELETTERSPACE.setObjectName("DRIVELETTERSPACE") DrivesData.addWidget(self.DRIVELETTERSPACE, key, 0) key += 1 Commented Oct 22, 2017 at 13:34
  • You have to add the widget to a different row and column, besides do not use self for a variable that is being updated in a loop since it could be eliminating the previous data. Commented Oct 22, 2017 at 13:36
  • This seems to be working. I think I need to assign a dynamic name as well. Any idea how could this be done(I'm going to try now). Commented Oct 22, 2017 at 14:06
  • 1
    you could create a list or a dictionary that is a member of the class where you store the variables. Commented Oct 22, 2017 at 14:08
  • Allow me to elaborate a bit. The previous loop created a new widget for each drive in the loop. I can see in gui that there's extra space created. I also have another loop for displaying the disk letters in those widgets. Here's how it looks like: for disk in disksInfo: diskLetter = disk.Caption DRIVELETTERSPACE.setText(_translate("MainWindow", diskLetter)) Commented Oct 22, 2017 at 14:10

1 Answer 1

1

In your code you have 2 errors, the first case is that you are adding the widget to the same position for it you must create some method to be able to vary the indices; the other error is to create a member of the class as iterator, for this you just delete self, for example:

key = 0 
for disk in disksInfo: 
    DRIVELETTERSPACE = QtWidgets.QLabel(self.gridLayoutWidget_3) 
    DRIVELETTERSPACE.setObjectName("DRIVELETTERSPACE") 
    DrivesData.addWidget(DRIVELETTERSPACE, key, 0) 
    key += 1

If you want to save the widget it is advisable to save them in a list or dictionary since later we can obtain them through an index or key, respectively.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.