Problem with rounding the item
-
There is a code:
def buttonLast(self): row = self.tableWidget_3.rowCount() if not row: return column = 1 item = self.tableWidget_3.item(row - 1, column) print(round(item))
which selects the last value in the pole
tableWidget
♪But on the team.
print(round(item))
I'm getting a mistake.Traceback (most recent call last): File "G:\PyCharm\SckethPython\pythonProject_x32_3_8_6\SL_lke2.1.py", line 138, in buttonLast print(round(item)) TypeError: type QTableWidgetItem doesn't define __round__ method
-
What are you going to do?
round
- That's it.QTableWidgetItem object
import sys from PyQt5.Qt import *
class Example(QDialog):
def init(self):
super().init()self.mytable = QTableWidget(4, 2) layout = QVBoxLayout(self) layout.addWidget(self.mytable) btn = QPushButton("Click me") btn.clicked.connect(self.buttonLast) layout.addWidget(btn) for i in range(4): for j in range(2): item = QTableWidgetItem("{}.{}123000".format(i, j)) item.setTextAlignment(Qt.AlignHCenter) self.mytable.setItem(i, j, item) def buttonLast(self): row = self.mytable.rowCount() if not row: return column = 1 item = self.mytable.item(row - 1, column) print(f'\nitem --> {item}\n') # --> QTableWidgetItem object at 0x0...> if item: item = item.text() # --> str item = float(item) # --> float print(round(item))
if name == 'main':
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())