我正在尝试按照以下示例创建位于此处的表: https://pythonbasics.org/pyqt-table/
完全承认,我是python的新手,这对我来说有点陌生。
我已经编辑字典以匹配我的数据,但是在计算机上填充表格时,这些值为空白。
我不知道哪里出了问题。
任何帮助将不胜感激。
下面的代码片段:
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
x = []
labels = []
for i in range(sheet.nrows-3):
print(sheet.row_values(i+3))
x.append(sheet.row_values(i+3))
labels.append(str(x[i][0])+" "+str(x[i][1]))
z={}
keypieces = range(len(x))
#Making the Z dictionary
for i in keypieces:
z[labels[i]] = x[i][4:7]
class TableView(QTableWidget):
def __init__(self, z, *args):
QTableWidget.__init__(self, *args)
self.z = z
self.setz()
self.resizeColumnsToContents()
self.resizeRowsToContents()
def setz(self):
horHeaders = []
for n, key in enumerate(sorted(self.z.keys())):
horHeaders.append(key)
for m, item in enumerate(self.z[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def main(args):
app = QApplication(args)
table = TableView(z, 3, 6)
table.show()
sys.exit(app.exec_())
if __name__=="__main__":
main(sys.argv)
我的z字典如下:
{'LEVEL 6 S1': [24.4999999999989, 5.00000000000394, 1.5],
'LEVEL 5 S1': [25.4999999999992, 4.99999999999996, 1.5],
'LEVEL 4 S1': [25.4999999999992, 4.99999999999996, 1.5],
'LEVEL 3 S1': [25.4999999999992, 3.41666666666662, 1.5],
'LEVEL 2 S1': [25.4999999999992, 3.91666666666663, 3.0],
'LEVEL 1 S1': [25.4999999999992, 4.99999999999996, 1.33333333333333]}
我的桌子看起来像这样(红色是我希望桌子看起来像的一个例子):
答案 0 :(得分:0)
主要错误是QTableWidgetItem仅支持字符串,另一方面,用于访问信息的方法是多余的。他们认为上述解决方案是:
def setz(self):
horHeaders = []
for j, (key, values) in enumerate(sorted(self.z.items())):
horHeaders.append(key)
for i, value in enumerate(values):
newitem = QTableWidgetItem(str(value))
# or
# newitem = QTableWidgetItem()
# newitem.setData(Qt.DisplayRole, value)
self.setItem(i, j, newitem)
self.setHorizontalHeaderLabels(horHeaders)
更新:
如果要支持数值和字符串,则必须使用以下角色(Qt :: DisplayRole或Qt :: UserRole):
import sys
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
from PyQt5.QtCore import Qt
class TableView(QTableWidget):
def __init__(self, z, *args):
super(TableView, self).__init__(*args)
self.z = z
self.setz()
self.resizeColumnsToContents()
self.resizeRowsToContents()
def setz(self):
horHeaders = []
for j, (key, values) in enumerate(sorted(self.z.items())):
horHeaders.append(key)
for i, value in enumerate(values):
newitem = QTableWidgetItem()
newitem.setData(Qt.EditRole, value)
self.setItem(i, j, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def main(args):
z = {
"LEVEL 6 S1": [24.4999999999989, 5.00000000000394, 1.5],
"LEVEL 5 S1": [25.4999999999992, 4.99999999999996, 1.5],
"LEVEL 4 S1": [25.4999999999992, 4.99999999999996, 1.5],
"LEVEL 3 S1": [25.4999999999992, 3.41666666666662, 1.5],
"LEVEL 2 S1": [25.4999999999992, 3.91666666666663, 3.0],
"LEVEL 1 S1": [25.4999999999992, 4.99999999999996, 1.33333333333333],
}
app = QApplication(args)
table = TableView(z, 3, 6)
table.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
如果相反,如果您希望默认情况下强制所有创建的列支持浮点值,则必须在委托中设置适当的编辑器,例如QDoubleSpinBox,
import sys
from PyQt5.QtWidgets import (
QApplication,
QDoubleSpinBox,
QStyledItemDelegate,
QTableWidget,
QTableWidgetItem,
)
from PyQt5.QtCore import Qt
class Delegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
DBL_MAX = 1.7976931348623157e308
editor = QDoubleSpinBox(parent, minimum=-DBL_MAX, maximum=DBL_MAX, decimals=323)
return editor
class TableView(QTableWidget):
def __init__(self, z, *args):
super(TableView, self).__init__(*args)
self.z = z
self.setz()
self.resizeColumnsToContents()
self.resizeRowsToContents()
delegate = Delegate(self)
self.setItemDelegate(delegate)
def setz(self):
horHeaders = []
for j, (key, values) in enumerate(sorted(self.z.items())):
horHeaders.append(key)
for i, value in enumerate(values):
newitem = QTableWidgetItem()
newitem.setData(Qt.EditRole, value)
self.setItem(i, j, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def main(args):
z = {
"LEVEL 6 S1": [24.4999999999989, 5.00000000000394, 1.5],
"LEVEL 5 S1": [25.4999999999992, 4.99999999999996, 1.5],
"LEVEL 4 S1": [25.4999999999992, 4.99999999999996, 1.5],
"LEVEL 3 S1": [25.4999999999992, 3.41666666666662, 1.5],
"LEVEL 2 S1": [25.4999999999992, 3.91666666666663, 3.0],
"LEVEL 1 S1": [25.4999999999992, 4.99999999999996, 1.33333333333333],
}
app = QApplication(args)
table = TableView(z, 3, 6)
table.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)