[PyQt] AttributeError: 'NoneType' object has no attribute 'text' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] AttributeError: 'NoneType' object has no attribute 'text' (/thread-35046.html) |
AttributeError: 'NoneType' object has no attribute 'text' - speedev - Sep-25-2021 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'[attachment=1282] What is the error reason and how can I solve it? RE: AttributeError: 'NoneType' object has no attribute 'text' - Yoriz - Sep-25-2021 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?
RE: AttributeError: 'NoneType' object has no attribute 'text' - speedev - Sep-25-2021 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. RE: AttributeError: 'NoneType' object has no attribute 'text' - Yoriz - Sep-25-2021 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 ?
RE: AttributeError: 'NoneType' object has no attribute 'text' - speedev - Sep-25-2021 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. RE: AttributeError: 'NoneType' object has no attribute 'text' - Axel_Erfurt - Sep-25-2021 Quote:I am trying to pull item content from PyQT5 table with this code 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) RE: AttributeError: 'NoneType' object has no attribute 'text' - speedev - Sep-25-2021 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 rangeHow can I solve it? RE: AttributeError: 'NoneType' object has no attribute 'text' - Axel_Erfurt - Sep-25-2021 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) RE: AttributeError: 'NoneType' object has no attribute 'text' - speedev - Sep-25-2021 (Sep-25-2021, 05:02 PM)Axel_Erfurt Wrote: If you don't need row and column you can use simple I click on a data from the table, but the print function does not work. It doesn't give an error either. RE: AttributeError: 'NoneType' object has no attribute 'text' - Axel_Erfurt - Sep-25-2021 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_()) |