How to fix string index out of range errror
Solved
Qt for Python
-
I'm trying to display details in a QLabel but when I try I get the following error, string index out of range. Below is the code that generated the error, I would appreciate assistance. Thanks
self.ui.tableWidget.cellClicked.connect(self.singleClick) def singleClick(self): try: data = self.ui.tableWidget.currentItem().text() id = data.split("-")[0] query = ("SELECT * FROM temporary_staff WHERE id_no=?") person = cur.execute(query, (id,)).fetchone() ######### single item tuple=(1,) label_pixmap = QPixmap("img/" + data[16]) self.ui.label_15.setPixmap(QPixmap(label_pixmap)) self.ui.label_15.repaint() self.ui.label_2.setText(data[1]) self.ui.label_4.setText(data[3]) self.ui.label_6.setText(data[4]) self.ui.label_8.setText(data[12]) self.ui.label_10.setText(data[15]) except Exception as e: print(e)
-
Hi,
You assume that is at least 15 char width. That is the issue with assumptions: they are often incorrect. Don't trust them, add proper checks to ensure that your next operations don't fail.