Python Forum
[PyQt] AttributeError: 'NoneType' object has no attribute 'text'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] AttributeError: 'NoneType' object has no attribute 'text'
#1
Hi,

        for x in secilen.indexes():
            satirlar = '{ "row" : 2, "column": 2 }'
            asdds = json.loads(satirlar)
            tablobul = self.ui.eklenenlerTablo.item(asdds["row"], asdds["column"])
            tets = tablobul.text()
            print(tets)
I am trying to pull item content from PyQT5 table with this code. But
tets = tablobul.text()
line gives an error.

AttributeError: 'NoneType' object has no attribute 'text'
   
What is the error reason and how can I solve it?
Reply
#2
What is self.ui.eklenenlerTablo.item because calling it with the current arguments is returning None
Under what conditions should it return None, and under what conditions should it return an object that has a text method?
Reply
#3
I can't understand what you are saying. Can you give the correct code? I would appreciate if you let me know where you made changes.
Reply
#4
I am saying you have not provided enough information to be able to help you solve the issue.
I cannot give the correct code, the code shown does not show what self.ui.eklenenlerTablo.item is.
There is no code that I made changes to

Is self.ui.eklenenlerTablo a pyqt table ?
If so are you calling its item method with a valid row & col values ?
Reply
#5
JSON contains row and column information. I want to pull information from table, with corresponding values ​​according to information. For example, if I wrote 2 for column by column in json, I want to pull the column corresponding to 2 from the table. Same for the row.
Reply
#6
Quote:I am trying to pull item content from PyQT5 table with this code

tets = tablobul.text()

What means PyQT5 table ?

QTableView or QTableWidget ?

You cannot use text() to get values

For a QTableWidget you can use something like that to get the selected Item text

row =  self.table.selectionModel().selectedIndexes()[0].row()
column = self.table.selectionModel().selectedIndexes()[0].column()
item = self.table.selectedItems()[0]
if not item == None:
    name = item.text()
else:
    name = ""
print(name)
Reply
#7
Thanks for reply.

There are problems with the following codes from the codes you provided:
        row =  self.ui.eklenenlerTablo.selectionModel().selectedIndexes()[0].row()
        column = self.ui.eklenenlerTablo.selectionModel().selectedIndexes()[0].column()
Error:

Traceback (most recent call last):
  File "c:\Users\emiry\Desktop\TamirProgram²\Arayuz.py", line 90, in secildiTablo
    column = self.ui.eklenenlerTablo.selectionModel().selectedIndexes()[0].column()
IndexError: list index out of range
How can I solve it?
Reply
#8
If you don't need row and column you can use simple

item = self.table.selectedItems()[0]
if not item == None:
    name = item.text()
else:
    name = ""
print(name)
Reply
#9
(Sep-25-2021, 05:02 PM)Axel_Erfurt Wrote: If you don't need row and column you can use simple

item = self.table.selectedItems()[0]
if not item == None:
    name = item.text()
else:
    name = ""
print(name)

I click on a data from the table, but the print function does not work. It doesn't give an error either.
Reply
#10
You must connect it to the function

Example

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QDropEvent
from PyQt5.QtWidgets import (QTableWidget, QAbstractItemView, QTableWidgetItem, 
                             QHBoxLayout, QApplication, QMainWindow)


class TableWidgetDragRows(QTableWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setDragEnabled(True)
        self.setAcceptDrops(True)
        self.viewport().setAcceptDrops(True)
        self.setDragDropOverwriteMode(False)
        self.setDropIndicatorShown(True)

        self.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.setSelectionBehavior(QAbstractItemView.SelectItems)
        self.setDragDropMode(QAbstractItemView.InternalMove)

    def dropEvent(self, event: QDropEvent):
        if not event.isAccepted() and event.source() == self:
            drop_row = self.drop_on(event)

            rows = sorted(set(item.row() for item in self.selectedItems()))
            rows_to_move = [[QTableWidgetItem(self.item(row_index, column_index)) for column_index in range(self.columnCount())]
                            for row_index in rows]
            for row_index in reversed(rows):
                self.removeRow(row_index)
                if row_index < drop_row:
                    drop_row -= 1

            for row_index, data in enumerate(rows_to_move):
                row_index += drop_row
                self.insertRow(row_index)
                for column_index, column_data in enumerate(data):
                    self.setItem(row_index, column_index, column_data)
            event.accept()
            for row_index in range(len(rows_to_move)):
                self.item(drop_row + row_index, 0).setSelected(True)
                self.item(drop_row + row_index, 1).setSelected(True)
        super().dropEvent(event)

    def drop_on(self, event):
        index = self.indexAt(event.pos())
        if not index.isValid():
            return self.rowCount()

        return index.row() + 1 if self.is_below(event.pos(), index) else index.row()

    def is_below(self, pos, index):
        rect = self.visualRect(index)
        margin = 2
        if pos.y() - rect.top() < margin:
            return False
        elif rect.bottom() - pos.y() < margin:
            return True
        # noinspection PyTypeChecker
        return rect.contains(pos, True) and not (int(self.model().flags(index)) & Qt.ItemIsDropEnabled) and pos.y() >= rect.center().y()


class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()

        self.table_widget = TableWidgetDragRows()
        self.table_widget.itemClicked.connect(self.get_item)
        self.setCentralWidget(self.table_widget) 

        self.table_widget.setColumnCount(2)
        self.table_widget.setHorizontalHeaderLabels(['Type', 'Name'])

        items = [('Red', 'Toyota'), ('Blue', 'RV'), ('Green', 'Beetle'), ('Silver', 'Chevy'), ('Black', 'BMW')]
        self.table_widget.setRowCount(len(items))
        for i, (color, model) in enumerate(items):
            self.table_widget.setItem(i, 0, QTableWidgetItem(color))
            self.table_widget.setItem(i, 1, QTableWidgetItem(model))

        
        self.statusBar().showMessage("Ready", 0)
        self.resize(400, 400)
        self.show()
        
    def get_item(self):
        item = self.table_widget.selectedItems()[0]
        if not item == None:
            name = item.text()
        else:
            name = ""
        print(name)
        self.statusBar().showMessage(name, 0)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Glow text of clickable object on hover with transition andy 6 1,303 May-11-2021, 07:39 AM
Last Post: andy
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 5,140 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 2,580 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 3,233 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 4,085 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  [Tkinter] AttributeError: 'App' object has no attribute 'set_text' Sahil1313 6 7,959 Jun-17-2018, 05:01 AM
Last Post: woooee
  [Kivy] AttributeError setting the label text gio123 3 3,565 Mar-15-2018, 09:30 AM
Last Post: buran
  'list' object has no attribute 'plot' problem BaartCM 2 11,384 Feb-03-2018, 09:20 PM
Last Post: BaartCM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020