Problem loading QTableWidget

2

I want to load the data of a table, the problem arises when I press the button it throws me the following error:

  

C: \ Users \ framework \ Downloads \ Python \ PyQt> python new.py   TypeError: loadData () takes 0 positional arguments but 2 were given

This is my code:

from PyQt4 import QtCore, QtGui
import sqlite3
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def loadData():
        connection = sqlite3.connect("condo2.db")
        query = "SELECT * FROM aptos"
        result =connection.execute(query)
        self.tableWidget.setRowCount(0)
        for row_number, row_data in enumerate(result):
            self.tableWidget.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))

        connection.close()
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.lista = QtGui.QTableWidget(self.centralwidget)
        self.lista.setGeometry(QtCore.QRect(30, 30, 721, 341))
        self.lista.setRowCount(111)
        self.lista.setColumnCount(4)
        self.lista.setObjectName(_fromUtf8("lista"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(300, 460, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.clicked.connect(self.loadData)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "cerrar", None))


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

Use Python 3.4 with PyQt 4.

    
asked by Marco Salazar 02.10.2017 в 15:22
source

1 answer

1

The error is thrown basically because you call loadData() as an instance method:

self.pushButton.clicked.connect(self.loadData)
                                ^^^^^

In fact, it should be an instance method (since you inherit and modify attributes of your class), the problem is that to be an instance method, you must receive as the first parameter the instance of the class to which it belongs. Generally, self is used per convention to name this argument. You do not specify the self parameter when declaring it but if you pass it when you call it with self.loadData() .

The QPushButton.clicked event sends a second argument which is the state of the button. You could omit it but it is important to remember in some cases how to use lambda to pass arguments, for example. In short you must declare the method as def loadData(self, state) . To learn more about the meaning of self look at yourself:

What is self used for in POO in Python?

You have other errors in this method:

  • The attribute self.tableWidget does not exist, I guess you should refer to self.lista , which is your instance of QTableWidget .

  • QTableWidgetItem in PyQt 4 is not found within the module QtWidgets (this is true only for PyQt 5 onwards) but within QtGui .

The loadData method must be:

def loadData(self, state):
    with sqlite3.connect('condo2.db') as con:
        cursor = con.cursor()
        cursor.execute("SELECT * FROM aptos")
        self.lista.setRowCount(0)
        for row_number, row_data in enumerate(cursor):
            self.lista.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.lista.setItem(row_number, column_number, QtGui.QTableWidgetItem(str(data)))
                self.lista.resizeColumnToContents(column_number)

The rest of the code is correct.

Edit:

The error:

  

sqlite3.OperationalError: no such table: aptos.

indicates that the table does not exist.

Make sure that the original database is in the same directory as your pyqt script (same folder). Otherwise, the appropriate absolute or relative path passes. If you have executed the script in another directory where the file of the original database was not (or the name does not match), to l not find the database you create a database with that name but empty and connects to it Of course, it has no table.

The error:

  

sqlite3.DatabaseError: file is encrypted or is not a database

is very often due to using a different version of SQLite than the API of your version of Python uses. Commonly due to modify or create the database with unsupported versions of sqlite CLI, C ++ API, etc. If it is due to this, the simplest thing is to create or modify the bd from Python itself.

    
answered by 02.10.2017 / 16:09
source