import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self): super().__init__() self.title = 'PyQt5 table - pythonspot.com' self.left = 0 self.top = 0 self.width = 300 self.height = 200 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.createTable()
self.layout = QVBoxLayout() self.layout.addWidget(self.tableWidget) self.setLayout(self.layout)
self.show()
def createTable(self): self.tableWidget = QTableWidget() self.tableWidget.setRowCount(4) self.tableWidget.setColumnCount(2) self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)")) self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)")) self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (2,1)")) self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (2,2)")) self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (3,1)")) self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (3,2)")) self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (4,1)")) self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (4,2)")) self.tableWidget.move(0,0)
self.tableWidget.doubleClicked.connect(self.on_click)
@pyqtSlot() def on_click(self): print("\n") for currentQTableWidgetItem in self.tableWidget.selectedItems(): print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text()) if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_())
|
Leave a Reply:
Thank you for the excellent post! However I am having an issue with the above script. When I try to run the same script and attempt a double click, the GUI gives a "Python has stopped working" and I have to force close the GUI then. Can you please look into this? Thanks in advance!
Try to reinstall PyQt