Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I was trying to work with TableWidgets on Python and I ran into an issue.

I wanted to check whether the table is filled or not (with str of course).

def add_table (self):
    self.kala = self.comboBox.currentText()
    self.code_kala = self.comboBox.currentIndex()
    self.vahed = self.comboBox_2.currentText()
    list_e = []
    for i in list(range(10)):
        #self.tab = self.tableWidget.item(i,0)
        if self.tableWidget.item(i,0).text() != '':
        #if self.tab.text() !='':
            list_e.append(i)
        else:
    self.ROW = len(list_e)
    self.tableWidget.setItem(self.ROW,0,QTableWidgetItem(self.kala))
    self.tableWidget.setItem(self.ROW,1,QTableWidgetItem(str(self.code_kala)))
    self.tableWidget.setItem(self.ROW,2,QTableWidgetItem(str(self.vahed)))

and I don't know why I keep getting this error:

NoneType' object has no attribute 'text'

Does anyone know how to solve it?

Also, I know this code doesn't have any problem (I got good results with the same code in another project) but as cmd said:

File "D:\**\***\*****\*******\*\*************.py", line 1755, in add_table
    if self.tableWidget.item(i,0).text() != '':
AttributeError: 'NoneType' object has no attribute 'text'
                NoneType Means the variable has nothing in it. You could use a debugger to check why the variable is empty.
– Vulpex
                Mar 30, 2019 at 14:05
                You could print the object just before the conditional statement and that should give you a clue which item is missing. Or you could Catch the error and inspect/print relevant data in the except suite. One of your table items is empty: if you expect to have empty cells(?) catch the error and pass in the except suite.
– wwii
                Mar 30, 2019 at 14:27
                As an aside: you can iterate over a range object without making a list from it : for i in list(range(10)): -->> for i in range(10):.  And I was looking through some SO Q&A's like this one - seems that Qtables(?) have a rowCount() method so the for statement becomes - for i in self.tableWidget.rowCount().
– wwii
                Mar 30, 2019 at 14:47
                @wwii ty man i found my problem with Your solution i used print function and i got None after 10 times ' ' so i changed the range(10) to range(9) and solved it
– PHM La
                Mar 30, 2019 at 15:25

It seems that the table widget might contain 'None' values, so you have to expect them to pop up. Instead of doing this:

    if self.tableWidget.item(i,0).text() != '':

do that:

    thing = self.tableWidget.item(i,0)
    if thing is not None and thing.text() != '':
        # do stuff with thing
                You Are amazing man  i can stop  this bug with your code ( however i found the problem) but i think your solution is Temporary because its not solving the problem it just dealing with it(Get off with it) and the actuall problem was range(10) i had to change it to 9 its funny how hard programming is
– PHM La
                Mar 30, 2019 at 15:18
                I am glad it was useful to you. Programming is not so much achieving end results as it is understanding all the building blocks and the tools to get you there. From now on, you will be able to recognize the pattern AttributeError: 'NoneType' object has no attribute 'text'
– sleblanc
                Mar 30, 2019 at 16:44
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.