N
I don't know exactly what the nature of the error you see, but I believe it is by some incompatibility between the Qt and Python thread system. The ideal is that when using PyQt, you use the threading system provided by them. There are several tutorials out there showing the different PyQt shapes and modules that you can use to run a function on a separate thread of the interface - for example, https://www.mfitzp.com/tutorials/multithreading-pyqt-applications-qthreadpool/ and https://realpython.com/python-pyqt-qthread/ (links in English).Below is a minimum example showing a similar interface to yours, which updates the counter displayed every 2 seconds (I have created something similar since I have no access to your .ui file):import sys
from PyQt5 import QtWidgets as qtw, QtCore as qtc
class MyApp(qtw.QWidget):
def init(self):
super().init()
# Contador que atualiza ao chamar self.update
self.a = 0 # valor inicial
# Layout do meu app
layout = qtw.QGridLayout()
self.setLayout(layout)
# Label estático "Monitoramento"
self.monitoramento_label = qtw.QLabel("Monitoramento")
layout.addWidget(self.monitoramento_label, 0, 0, 1, 2)
# Label estático "Valor de A"
self.valor_a_label = qtw.QLabel("Valor de A")
layout.addWidget(self.valor_a_label, 1, 0, 1, 1)
# Tabela
self.table = qtw.QTableWidget(rowCount=1, columnCount=2)
self.table.setHorizontalHeaderLabels(["Nome", "Valor"])
self.table.setItem(0, 0, qtw.QTableWidgetItem("Valor de A"))
self.table.setItem(0, 1, qtw.QTableWidgetItem(str(self.a)))
layout.addWidget(self.table, 1, 1, 2, 1)
# Label dinâmico - exibe o valor de self.a
self.a_label = qtw.QLabel(str(self.a))
layout.addWidget(self.a_label, 2, 0, 1, 1)
# Timer - responsável por chamar o método self.update
# a cada 2000 milissegundos (2 segundos)
self.timer = qtc.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(2000)
def update(self):
"""Incrementa self.a em um e atualiza a interface em seguida."""
self.a += 1
self.a_label.setText(str(self.a))
self.table.setItem(0, 1, qtw.QTableWidgetItem(str(self.a)))
if name == 'main':
app = qtw.QApplication(sys.argv)
gui = MyApp()
gui.show()
sys.exit(app.exec_())
Result (if you run the code, you will see the counter being incremented):
The secret here is in the object QTimer, in the lines:self.timer = qtc.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(2000)
Here, we create the object, we connect it to the method self.update and we ask him to call this method every 2000 milliseconds (2 seconds).Note that the code I wrote involves the definition of a class MyApp and their methods init and update - If you are not familiar with these things, I recommend studying more about object-oriented programming. It will help you quite understand better about PyQt and Python in general.Note also that I wrote the interface elements (the labels, the table etc) directly in the code. In this way, it is not necessary to use Qt Designer to mount the interface (this is a bit of personal preference, but I prefer to write the entire interface code because it is easier to locate me when the interface starts to get more complex).