Good day community. I have a very simple code that makes a loop when pressing the button of a window created with PyQt5. When pressing the button, the window freezes. I have been reading several solutions that use thread to solve it, but none of them loaded ".ui" files and had the main process inside a single class. So, do you know any way to avoid the window freezing when the function to be executed is inside the main class?
Here is the code:
"basic.ui"
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>231</width>
<height>75</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
"code.py"
from PyQt5 import QtWidgets, uic
import sys,time
GUI = uic.loadUiType(r'basic.ui')[0]
class MainWindowClass(QtWidgets.QMainWindow, GUI):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.pushButton.clicked.connect(lambda: self.MainProcess())
def MainProcess(self):
count=0
while count!=100:
print(count)
count=count+1
time.sleep(3)
app = QtWidgets.QApplication(sys.argv)
MyDialog = MainWindowClass(None)
MyDialog.show()
app.exec_()
Thanks in advance!