gpt4 book ai didi

python - Pyqt5:QtableWidgetItem "None"

转载 作者:行者123 更新时间:2023-12-01 08:12:11 26 4
gpt4 key购买 nike

我在 PyQt5 中创建了一个表,并通过此方法检索单元格中的数据。但是当我尝试 QTableWidgetItem 中的单元格变为 None 时。那么,我如何解决这个问题以及如何在特定单元格中插入其他确定的项目,例如项目 2、项目 3...谢谢

import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor
from PyQt5 import QtCore

data = {'111':['Title 1','121','94565','','','','684651','','','44651','','',''], '112':['Title 2','65115','','466149','46645','555641','','','','412045','98416','',''], '113':['Title 3','','','','466149','46645','555641','98656','','','412045','98416','','']}

class Table(QWidget):
def __init__(self, *args, parent=None):
super().__init__()
self.data = data
self.setuptUI()
k = 'str'
v = int
n = int
m = int
self.setdata(k, v, n, m)

def setuptUI(self):
self.setWindowTitle("QTableWidgetItem")
self.resize(1200, 800)
conLayout = QHBoxLayout()
self.tableWidget =QTableWidget(self)
self.tableWidget.setRowCount(55)
self.tableWidget.setColumnCount(14)
conLayout.addWidget(self.tableWidget)

def setdata(self, k, v, n, m):
global item
item = str(self.data.get(k))
for key in self.data:
if k in key:
item = self.data.get(k)[v]
print(item)
newItem = QTableWidgetItem(str(item))
newItem.setForeground(QBrush(QColor(255, 0, 0)))
self.tableWidget.setItem(23, 4, newItem)

if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table(data)
item1= Table()
item1.setdata(k="113", v=5, n=24, m=4)
#item2= Table()
#item2.setdata(k="113", v=5, n=25, m=4
windows.show()
sys.exit(app.exec_())

最佳答案

尝试一下:

import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor
from PyQt5 import QtCore

data = {'111':['Title 1','121','94565','','','','684651','','','44651','','',''],
'112':['Title 2','65115','','466149','46645','555641','','','','412045','98416','',''],
'113':['Title 3','','','','466149','46645','555641','98656','','','412045','98416','','']}

class Table(QWidget):
def __init__(self, data): # data
super().__init__()
self.data = data
self.setuptUI()

# k = 'str'
# v = int
# n = int
# m = int
# self.setdata(k, v, n, m)

def setuptUI(self):
self.setWindowTitle("QTableWidgetItem")
self.resize(1200, 600)

conLayout = QHBoxLayout(self) # + self
self.tableWidget = QTableWidget(self)
self.tableWidget.setRowCount(55)
self.tableWidget.setColumnCount(14)
conLayout.addWidget(self.tableWidget)

def setdata(self, k, v, n, m):
# global item # ---
# item = str(self.data.get(k))
# for key in self.data:
# if k in key:
item = self.data.get(k)[v]
print(item)

newItem = QTableWidgetItem(str(item))
newItem.setForeground(QBrush(QColor(255, 0, 0)))
# self.tableWidget.setItem(23, 4, newItem)
self.tableWidget.setItem(n, m, newItem)

if __name__ == '__main__':
app = QApplication(sys.argv)

windows = Table(data)
#- item1= Table()
#- item1.setdata(k="113", v=5, n=24, m=4)
windows.setdata(k="113", v=5, n=24, m=4)
windows.setdata(k="113", v=6, n=24, m=5) # v=6, m=5

windows.show()
sys.exit(app.exec_())

enter image description here

关于python - Pyqt5:QtableWidgetItem "None",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55171115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com