import sys
from PyQt5.
QtGui import
QIconfrom PyQt5.QtCore import pyqtSlot, Qt
def __init__(self):
super().__init__()
self.title = 'PyQt5 table - pythonspot.com'
self.left = 200
self.top = 200
self.width = 800
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add box layout, add table to box layout and add box layout to widget
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
# Show widget
self.show()
def createTable(self):
# Create table
self.tableWidget.setRowCount(2)
self.tableWidget.setColumnCount(6)
self.tableWidget.setHorizontalHeaderLabels(["algo", "algo", "algo", "algo", "algo", "algo"])
registros = ["Celda", "Celda", "Celda", "Celda", "Celda", "Celda"]
# BUGGED ROW
for dato in registros:
self.
tableWidget.
setItem(0, registros.
index(dato
),
QTableWidgetItem(registroitem
)) # NON BUGGED ROW
if __name__ == '__main__':
ex = App()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot, Qt
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 table - pythonspot.com'
self.left = 200
self.top = 200
self.width = 800
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
# Show widget
self.show()
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(2)
self.tableWidget.setColumnCount(6)
self.tableWidget.setHorizontalHeaderLabels(["algo", "algo", "algo", "algo", "algo", "algo"])
registros = ["Celda", "Celda", "Celda", "Celda", "Celda", "Celda"]
# BUGGED ROW
for dato in registros:
registroitem = QTableWidgetItem(dato)
self.tableWidget.setItem(0, registros.index(dato), QTableWidgetItem(registroitem))
# NON BUGGED ROW
self.tableWidget.setItem(1,0, QTableWidgetItem("Celda"))
self.tableWidget.setItem(1,1, QTableWidgetItem("Celda"))
self.tableWidget.setItem(1,2, QTableWidgetItem("Celda"))
self.tableWidget.setItem(1,3, QTableWidgetItem("Celda"))
self.tableWidget.setItem(1,4, QTableWidgetItem("Celda"))
self.tableWidget.setItem(1,5, QTableWidgetItem("Celda"))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks