如何使用PyQt5在QTableWidget中添加和检索项目?

时间:2016-11-26 06:42:15

标签: python python-3.x pyqt pyqt5 qtablewidget

我正在使用Python 3.4和PyQt5。

我是python和Qt的新手。我在使用Qt Designer创建的GUI中遇到问题,特别是setItem()item()函数我编写了两个函数,每个函数都由一个按钮执行。

'addRow(self)'使用QTableWidget中的值创建并填充QLineEdit上的行。

'sumCol(self)'循环显示每一行,将列中的每个项目添加到列表中,找到总计,然后打印到QLineEdit

我收到以下错误:

AttributeError: 'module' object has no attribute 'QTableWidgetItem'

我的代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from test_1 import Ui_MainWindow

class TestApp(Ui_MainWindow):
    def __init__(self, dialog):
        Ui_MainWindow.__init__(self)
        self.setupUi(dialog)

        self.addButton.clicked.connect(self.addRow)
        self.sumColButton.clicked.connect(self.sumCol)

    def addRow(self):
        #Retrieve text from QLineEdit
        x = str(self.x_input.text())
        y = str(self.y_input.text())
        z = str(self.z_input.text())        
        #Create a empty row at bottom of table
        numRows = self.tableWidget.rowCount()
        self.tableWidget.insertRow(numRows)     
        #Add text to the row
        self.tableWidget.setItem(numRows, 0, QtGui.QTableWidgetItem(x))
        self.tableWidget.setItem(numRows, 1, QtGui.QTableWidgetItem(y))
        self.tableWidget.setItem(numRows, 2, QtGui.QTableWidgetItem(z))

    def sumCol(self):
        #Create lists to hold values from each column
        xlist = []
        ylist = []
        zlist = []
        numRows = self.tableWidget.rowCount()
        for row in range(numRows):
            #Retreive item from the cell
            xitem = self.tableWidget.item(row, 0)
            yitem = self.tableWidget.item(row, 1)
            zitem = self.tableWidget.item(row, 2)
            #item to text
            x = float(xitem.text())
            y = float(yitem.text())
            z = float(zitem.text())
            #Add to list
            xlist.append(x)
            ylist.append(y)
            zlist.append(z)
        #Sum values in the column
        xsum = str(sum(xlist))
        ysum = str(sum(ylist))
        zsum = str(sum(zlist))
        #Display sum
        self.xdisp.setText(xsum)
        self.ydisp.setText(ysum)
        self.zdisp.setText(zsum)

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

    test_1 = TestApp(dialog)

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

GUI文件test_1.py:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.x_input = QtWidgets.QLineEdit(self.centralwidget)
        self.x_input.setGeometry(QtCore.QRect(30, 10, 61, 21))
        self.x_input.setObjectName("x_input")
        self.z_input = QtWidgets.QLineEdit(self.centralwidget)
        self.z_input.setGeometry(QtCore.QRect(30, 70, 61, 21))
        self.z_input.setObjectName("z_input")
        self.y_input = QtWidgets.QLineEdit(self.centralwidget)
        self.y_input.setGeometry(QtCore.QRect(30, 40, 61, 21))
        self.y_input.setObjectName("y_input")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(110, 10, 311, 161))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        self.addButton = QtWidgets.QPushButton(self.centralwidget)
        self.addButton.setGeometry(QtCore.QRect(20, 100, 75, 23))
        self.addButton.setObjectName("addButton")
        self.ydisp = QtWidgets.QLineEdit(self.centralwidget)
        self.ydisp.setGeometry(QtCore.QRect(540, 50, 61, 21))
        self.ydisp.setObjectName("ydisp")
        self.xdisp = QtWidgets.QLineEdit(self.centralwidget)
        self.xdisp.setGeometry(QtCore.QRect(470, 50, 61, 21))
        self.xdisp.setObjectName("xdisp")
        self.zdisp = QtWidgets.QLineEdit(self.centralwidget)
        self.zdisp.setGeometry(QtCore.QRect(620, 50, 61, 21))
        self.zdisp.setObjectName("zdisp")
        self.sumColButton = QtWidgets.QPushButton(self.centralwidget)
        self.sumColButton.setGeometry(QtCore.QRect(460, 20, 75, 23))
        self.sumColButton.setObjectName("sumColButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "x"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "y"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "z"))
        self.addButton.setText(_translate("MainWindow", "Add Row"))
        self.sumColButton.setText(_translate("MainWindow", "Sum Column"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:2)

QTableWidgetItem是一个小部件,必须从QWidgets

导入

更改

def addRow(self):
    #Retrieve text from QLineEdit
    x = str(self.x_input.text())
    y = str(self.y_input.text())
    z = str(self.z_input.text())        
    #Create a empty row at bottom of table
    numRows = self.tableWidget.rowCount()
    self.tableWidget.insertRow(numRows)     
    #Add text to the row
    self.tableWidget.setItem(numRows, 0, QtGui.QTableWidgetItem(x))
    self.tableWidget.setItem(numRows, 1, QtGui.QTableWidgetItem(y))
    self.tableWidget.setItem(numRows, 2, QtGui.QTableWidgetItem(z))

def addRow(self):
    # Retrieve text from QLineEdit
    x = self.x_input.text()
    y = self.y_input.text()
    z = self.z_input.text()
    # Create a empty row at bottom of table
    numRows = self.tableWidget.rowCount()
    self.tableWidget.insertRow(numRows)
    # Add text to the row
    self.tableWidget.setItem(numRows, 0, QtWidgets.QTableWidgetItem(x))
    self.tableWidget.setItem(numRows, 1, QtWidgets.QTableWidgetItem(y))
    self.tableWidget.setItem(numRows, 2, QtWidgets.QTableWidgetItem(z))

输出:

enter image description here

答案 1 :(得分:0)

(代表OP发布)

解决方案是使用QtWidget.QTableWidgetItem()而不是QtGui.QTableWidgetItem()。