1

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!

Sadae
  • 93
  • 10
  • The real question is: what do you *actually* need to do in that while loop? – musicamante Aug 31 '22 at 00:14
  • The truth is that I have developed a rather extensive code with this structure. From the beginning I haven't had any problem with the main window freezing, until now when I need to update some interface objects while the main code is running. That is, I just tried to simplify the code as much as possible to show it in stack and see if there is any solution... – Sadae Aug 31 '22 at 03:11
  • don't use `while`-loop in any GUI (Tkinter, PyQt, wxPython, Kivy) in any language because it blocks `main loop` which GUI has to run to get key/mouse events from system, send them to widgets, update widgets, and redraw window and widgets. Long-running code you will have to run in separated thread (using `threading.Thread` or better `QThread` in `PyQt`). If you have to run loop with simple code like in your example then you could use timer to execute `MainProcess` every 3 second (without `while`-loop and `sleep`) and GUI will have time to run mainloop – furas Aug 31 '22 at 08:26
  • Stackoverflow: [python - Background thread with QThread in PyQt - Stack Overflow](https://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt). External tutorial: [Use PyQt's QThread to Prevent Freezing GUIs – Real Python](https://realpython.com/python-pyqt-qthread/) – furas Aug 31 '22 at 08:46

0 Answers0