W
W
WolfInChains2020-11-04 21:20:56
SQLite
WolfInChains, 2020-11-04 21:20:56

How to make sqlite and pyqt5 friends?

There are such pieces of code

Function in which you need to pass parameters from the database and display in the labels

def retranslateUi(self, MainWindow, city_name, city_s_name, type_of_government, attitude_to_magic, level_of_technology, natural_area, short_info):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.l_city_name.setText(_translate("MainWindow", f"{city_name}"))
        self.l_city_s_name.setText(_translate("MainWindow", f"{city_s_name}"))
        self.l_city_edit_info_1.setText(_translate("MainWindow", f"{type_of_government}"))
        self.l_city_edit_info_2.setText(_translate("MainWindow", f"{attitude_to_magic}"))
        self.l_city_edit_info_4.setText(_translate("MainWindow", f"{level_of_technology}"))
        self.l_city_edit_info_3.setText(_translate("MainWindow", f"{natural_area}"))
        self.l_city_edit_info_5.setText(_translate("MainWindow", f"{short_info}"))


How I tried to do it (these lines are in the setupUi function)
city = sq.get_city_info(2)
self.retranslateUi(MainWindow, city[1], city[2], city[3], city[4], city[5], city[6], city[7])


Function for getting data from the database
def get_city_info(city_id: int):
    cmd = "SELECT * FROM cities_info WHERE id = %d" % city_id
    c.execute(cmd)
    result = c.fetchone()[0]
    return result


When I try to click on the button, I get an error
Process finished with exit code -1073740791 (0xC0000409)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MasterCard000, 2020-11-04
@WolfInChains

If I'm not mistaken. You cannot change values ​​in a separate thread. You need to signal changes to the main thread.
Try to manage the signals, the code below is provided for a visual understanding of how the signal works.

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(406, 240)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 40, 251, 41))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        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"))
        self.label.setText(_translate("MainWindow", "TextLabel"))

class bb():
    def __init__(self, signal):
        signal.emit("Я здесь что то поменял\n и отправил в основной поток")

class main(QtWidgets.QMainWindow, Ui_MainWindow):
    signal = QtCore.pyqtSignal(str)
    def __init__(self, parent=None):
        super().__init__()
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.signal.connect(self.text, QtCore.Qt.QueuedConnection)
        bb(self.signal)
    def text(self, text):
        self.label.setText(text)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = main()
    MainWindow.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question