Issue
I have developed an application that accesses a database every 60 seconds and updates data in table by using PyQT5. But I can't do periodic table filling. The app does the calculation in a loop, but doesn't load. An example of my code:
from PyQt5 import QtCore, QtGui, QtWidgets
from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc:")
conn = engine.connect()
def AddFeatures(df):
pass
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.pc_3 = QtWidgets.QTextEdit(self.centralwidget)
self.pc_3.setGeometry(QtCore.QRect(90, 110, 101, 31))
self.pc_3.setObjectName("pc_3")
self.pc_4 = QtWidgets.QTextEdit(self.centralwidget)
self.pc_4.setGeometry(QtCore.QRect(190, 110, 101, 31))
self.pc_4.setObjectName("pc_4")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textEdit_2.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">4 клеть</span></p></body></html>"))
while(True):
df = pd.read_sql("SELECT TOP 200 FROM table")
AddFeatures(df)
self.pc_3.setText(str(df['Amp3'][1]))
self.pc_4.setText(str(df['Amp4'][1]))
time.sleep(60)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I would like to show this window
Solution
It seems like you are trying to run a loop that continuously reads data from a database and updates it. However, running an infinite loop like this within the PyQt5 main thread will cause your application to become unresponsive since the UI updates won't be processed while the loop is running.
To achieve periodic updates without freezing the GUI, you should use a QTimer to trigger the database update at regular intervals
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# Set up a timer to trigger the database update every 60 seconds
self.timer = QtCore.QTimer(MainWindow)
self.timer.timeout.connect(self.updateDatabase)
self.timer.start(60000) # 60,000 milliseconds = 60 seconds
Answered By - Aswath Cm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.