Pyqt5:QtableWidgetItem "None"
Pyqt5: QtableWidgetItem "None"
我在 PyQt5 中创建了一个 table 并通过此方法检索单元格中的数据。但是当我尝试 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_())
我在 PyQt5 中创建了一个 table 并通过此方法检索单元格中的数据。但是当我尝试 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_())