O
O
Oleg Batalov2021-05-05 15:49:05
Python
Oleg Batalov, 2021-05-05 15:49:05

Why does QLabel stop updating as time passes?

Hello. I am not a programmer. I'm trying to make a shell over the SKD database. The capabilities of standard software are not enough.

Every 500 milliseconds (precision is not important) a query is made to the database. If the data is updated, it is displayed on the screen.

The problem is that the GUI sometimes freezes. I tried to check by outputting the time to the application console and outputting the same values ​​to the combobox, printing and adding values ​​\u200b\u200bfor checking (I chose it because it was added to the screen). At a certain point in time the GUI stops updating even though the time keeps updating and being added inside the combobox.

The problem is most likely somewhere in the code, but due to the fact that I don’t have a lot of knowledge, I can’t figure it out myself.

UPD: I don’t know what is the reason, but it has been working for 25 minutes without freezing. Possible coincidence, but before that I added the output and adding elements to the combobox for the test.

from string import punctuation
import datetime
import numpy
import pyodbc
from PyQt5 import QtWidgets
import sys
from PyQt5.QtGui import QPixmap
import sqltestui
import threading


m = 0
newImage = 0

class ExampleApp(QtWidgets.QMainWindow, sqltestui.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.sqltest1)

        self.foo()

    def foo(self):
        global m
        global newImage
        testarray = []
        cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=DESKTOP-8EKCG28\RUSGUARD;DATABASE=RUSGUARDDB')
        cursor = cnxn.cursor()
        idNumber = cursor.execute('SELECT TOP(1) _id FROM RusGuardDB.dbo.Log where logmessagetype = 1 and logmessagesubtype = 66 ORDER BY _id DESC').fetchone()
        row_to_int = [int(x) for x in idNumber]
        if row_to_int[0] != m:
            m = row_to_int[0]
            newImage = newImage + 1
        empid = cursor.execute ('SELECT TOP(3) EmployeeID FROM RusGuardDB.dbo.Log where logmessagetype = 1 and logmessagesubtype = 66 ORDER BY _id DESC').fetchall()
        empfullid = numpy.array(empid)
        for i in range(len(empfullid)):
            g = str(empfullid[i])
            v = g.translate(str.maketrans('', '', punctuation))
            teste = cursor.execute(F"select [Фото 1] from RusGuardDB.dbo.EmployeesView where _id = '{v}'").fetchone()
            testarray.append(teste)
        cursor.close()
        name_array = numpy.array(testarray)
        #вывод в label1 последнее изображение
        for row in teste:
            row_to_list = [elem for elem in row]
        pixmap = QPixmap()
        pixmap1 = QPixmap()
        pixmap2 = QPixmap()
        pixmap.loadFromData(bytearray(name_array[0]))
        pixmap1.loadFromData(bytearray(name_array[1]))
        pixmap2.loadFromData(bytearray(name_array[2]))
        if newImage == 1:
            self.label1.setPixmap(pixmap.scaled(self.label1.size(), aspectRatioMode = 1))
            self.label1_2.setPixmap(pixmap1.scaled(self.label1_2.size(), aspectRatioMode = 1))
            self.label1_3.setPixmap(pixmap2.scaled(self.label1_3.size(), aspectRatioMode = 1))
        if newImage == 2:
            self.label1.setPixmap(pixmap.scaled(self.label1.size(), aspectRatioMode = 1))
            self.label1_2.setPixmap(pixmap1.scaled(self.label1_2.size(), aspectRatioMode = 1))
            self.label1_3.setPixmap(pixmap2.scaled(self.label1_3.size(), aspectRatioMode = 1))
        if newImage == 3:
            self.label1.setPixmap(pixmap.scaled(self.label1.size(), aspectRatioMode = 1))
            self.label1_2.setPixmap(pixmap1.scaled(self.label1_2.size(), aspectRatioMode = 1))
            self.label1_3.setPixmap(pixmap2.scaled(self.label1_3.size(), aspectRatioMode = 1))
            newImage = 0

        self.comboBox.addItem(str(datetime.datetime.now()))
        self.comboBox.setEditText(str(datetime.datetime.now()))
        print (str(datetime.datetime.now()))

        threading.Timer(0.5, self.foo).start()

def main():

    app = QtWidgets.QApplication(sys.argv)
    window = ExampleApp()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Batalov, 2021-05-06
@KPeoJI

Solved the hanging problem. Moved the GUI update to the main thread with pyqtSignal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question