python中的表小部件问题

时间:2012-02-27 19:05:57

标签: python pyqt

我基本上是在尝试搜索数组并使用搜索结果填充表格小部件。其他一切似乎都在工作但是当我执行我的代码并填写所有信息时,我不断收到一条错误消息,上面写着TypeError:list indices必须是整数,而不是str。这是我遇到问题的代码

if(studentRecord['Student Name'] == self.ui.SearchlineEdit.text()):
    studentsArray = self.loadstudentArrayOfRecords()
    self.ui.DetailstableWidget.insertRow(0)
    self.ui.DetailstableWidget.setItem(0,0,QtGui.QTableWidgetItem(studentsArray[self.ui.SearchlineEdit.text()]['Student Name']))
    self.ui.DetailstableWidget.setItem(0,1,QtGui.QTableWidgetItem(studentsArray[self.ui.SearchlineEdit.text()]['Student Code']))
    self.ui.DetailstableWidget.setItem(0,2,QtGui.QTableWidgetItem(studentsArray[self.ui.SearchlineEdit.text()]['Instrument Name']))
    self.ui.DetailstableWidget.setItem(0,3,QtGui.QTableWidgetItem(studentsArray[self.ui.SearchlineEdit.text()]['Instrument Code']))

1 个答案:

答案 0 :(得分:1)

看起来像

self.loadstudentArrayOfRecords()应该返回一个dicts的字典,但会返回一个列表的字典。

检查功能self.loadstudentArrayOfRecords()

如果您使代码更具可读性,您会发现错误更快:

searchText = self.ui.SearchlineEdit.text()
if studentRecord['Student Name'] == searchText:
    studentsArray = self.loadstudentArrayOfRecords()
    student = studentsArray[searchText]
    tableWidget = self.ui.DetailstableWidget
    tableWidget.insertRow(0)
    tableWidget.setItem(0, 0, QtGui.QTableWidgetItem(student['Student Name']))
    tableWidget.setItem(0, 1, QtGui.QTableWidgetItem(student['Student Code']))
    tableWidget.setItem(0, 2, QtGui.QTableWidgetItem(student['Instrument Name']))
    tableWidget.setItem(0, 3, QtGui.QTableWidgetItem(student['Instrument Code']))