PyQT表格GUI实现遇KeyError:"Parameter"报错求助
问题原因与解决方案
核心问题
你遇到的KeyError: "parameter",本质是表格模型逻辑与数据结构不兼容:
- 你定义的
headers是["parameter", "value"],模型的data方法会用这两个字符串作为键,去每行对应的字典里取值。 - 但你构造的
data是包含单个大字典的列表,这个字典的键是参数名(比如serialNumber),根本没有parameter或value这两个键,自然触发KeyError。
另外还有一个隐藏问题:ssDO_KEY_LOCK = int.from_bytes(bytes2[0x49:0x49], byteorder="little")里的切片0x49:0x49是空字节,会导致int.from_bytes报错,需要修正切片范围。
修正步骤
- 重构数据结构:把单个大字典拆分成多个小字典,每个小字典对应表格一行,结构为
{"parameter": "参数名", "value": 参数值}。 - 修正字节切片:调整
ssDO_KEY_LOCK的切片为有效范围(示例中取1字节,可根据实际需求调整)。 - 模型逻辑不变:重构后的数据结构匹配模型预期,模型代码无需修改。
修正后的完整代码
import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import * class DictionaryTableModel(QtCore.QAbstractTableModel): def __init__(self, data, headers): super(DictionaryTableModel, self).__init__() self._data = data self._headers = headers def data(self, index, role): if role == Qt.DisplayRole: column = index.column() column_key = self._headers[column] return self._data[index.row()][column_key] def rowCount(self, index): return len(self._data) def columnCount(self, index): return len(self._headers) def headerData(self, section, orientation, role): if role == Qt.DisplayRole: if orientation == Qt.Horizontal: return str(self._headers[section]) if orientation == Qt.Vertical: return str(section) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.table = QtWidgets.QTableView() with open("C:\\Ptests\\asdf\\bikeparametereditor\\uploaddomainobject.bin", 'rb') as file: bytes2 = file.read() serialNumber = bytes2[0x1b:0x1b + 15].decode("utf-8") frameNumber = bytes2[0x2a:0x3b].decode("utf-8") productionDate = int.from_bytes(bytes2[0x3b:0x3e], byteorder="little") hardwareVersion = bytes2[0x3f:0x49].decode("utf-8") # 修正切片范围,取1字节,可根据实际需求调整 ssDO_KEY_LOCK = int.from_bytes(bytes2[0x49:0x4a], byteorder="little") coreDataVersion = int.from_bytes(bytes2[0x00:0x02], byteorder="little") coreCRC = int.from_bytes(bytes2[0x03:0x06], byteorder="little") coreTimeStamp = int.from_bytes(bytes2[0x07:0x09], byteorder="little") coreAddrIndex = int.from_bytes(bytes2[0x09:0x11], byteorder="little") coreSize = int.from_bytes(bytes2[0x20:0x21], byteorder="little") coreWriteCounter = int.from_bytes(bytes2[0x21:0x22], byteorder="little") corefailCounter = int.from_bytes(bytes2[0x22:0x23], byteorder="little") # 重构数据结构:每个参数对应一行字典,匹配表头键 data = [ {"parameter": "serialNumber", "value": serialNumber}, {"parameter": "frameNumber", "value": frameNumber}, {"parameter": "productionDate", "value": productionDate}, {"parameter": "hardwareVersion", "value": hardwareVersion}, {"parameter": "coreDataVersion", "value": coreDataVersion}, {"parameter": "coreCRC", "value": coreCRC}, {"parameter": "coreTimeStamp", "value": coreTimeStamp}, {"parameter": "coreAddrIndex", "value": coreAddrIndex}, {"parameter": "coreSize", "value": coreSize}, {"parameter": "coreWriteCounter", "value": coreWriteCounter}, {"parameter": "corefailCounter", "value": corefailCounter}, {"parameter": "ssDO_KEY_LOCK", "value": ssDO_KEY_LOCK} ] headers = ["parameter", "value"] self.model = DictionaryTableModel(data, headers) self.table.setModel(self.model) self.setCentralWidget(self.table) app = QtWidgets.QApplication(sys.argv) window = MainWindow() window.show() app.exec_()
关键修改说明
- 拆分大字典为多个小字典,每个小字典对应表格一行,完美匹配模型用表头键取值的逻辑。
- 修正空字节切片问题,避免额外异常。
内容的提问来源于stack exchange,提问作者MikexCodes
相关产品推荐
相关产品推荐

