I used pyuic to compile a GUI file from Qt Designer, and I tried to figure out how to set all the cells in the first column with the same value "n"
.
The following is my code:
class Ui_MainWindow(object):
...
def accinit(self):
for n in xrange(9):
item = self.tableWidget.item(n, 0)
item.setText(_translate("MainWindow", "n", None))
Console ouput:
AttributeError: 'NoneType' object has no attribute 'setText'
I changed the code for only one cell (0,0), and it works perfectly:
def accinit(self):
item = self.tableWidget.item(0, 0)
item.setText(_translate("MainWindow", "n", None))
The cell at (0,0) has content "n"
.
I thought maybe that it's not allowed in a for loop, so I changed code to:
def accinit(self):
for x in xrange(1):
item = self.tableWidget.item(0, 0)
item.setText(_translate("MainWindow", "n", None))
But it still works! Why?
I already referenced the following articles (but I still can't solve this error):
Python: Attribute Error - 'NoneType' object has no attribute 'something'
Python AttributeError: NoneType object has no attribute 'close'
I am not exactly sure. But this could help.
It shows None Type object error
=> item is None after the tableWidget.item statement is executed
=> No item is present at (n, 0) for some n because of which it is returning none
But it works for (0, 0)
=> An item is already present at (0, 0)
=> Check if your tableWidget has headers? If it has headers there is a non-None item at (0, 0) and therefore you can set text for it. Therefore, there is an item only in (0, x), and it throws the NoneType error for (1, 0) and not for (0, 0).
Possible solution? Or correction
Create a new item, and use tableWidget.setItem(...) to do the needful. Something like
for x in xrange(5):
item = QTableWidgetItem()
item.setText("blah blah")
self.tableWidget.setItem(n, 0, item)