Python Forum

Full Version: Closing the Programm after stuck in while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am working on a small project which start after pressing a button. For this project I'm using Python (X, Y) and have created the graphical interface in QtDesigner.
For this I started the application at the beginning, opened and displayed the created window. Now I wait until the start button is pressed. Afterward the function just goes on in a while loop.
My problem now is that the project stops responding to the buttons and I can't stop the project anymore.
Is there a way to work around this or get rid of the while loop all together?

Greetings
Matthias
Without seeing the code, it's hard to help.
You must always avoid blocking your main loop, as it makes the GUI non responsive. For a long running task, you must place the code in a separate thread and communicate through qt signals and slots system.

#!/usr/bin/python3
# Threading example with QThread and moveToThread (PyQt5)
import sys
import time
from PyQt5 import QtWidgets, QtCore

class WorkerThread(QtCore.QObject):
    signalExample = QtCore.pyqtSignal(str, int)

    def __init__(self):
        super().__init__()

    @QtCore.pyqtSlot()
    def run(self):
        while True:
            # Long running task ...
            self.signalExample.emit("leet", 1337)
            time.sleep(5)

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.worker = WorkerThread()
        self.workerThread = QtCore.QThread()
        self.workerThread.started.connect(self.worker.run)  # Init worker run() at startup (optional)
        self.worker.signalExample.connect(self.signalExample)  # Connect your signals/slots
        self.worker.moveToThread(self.workerThread)  # Move the Worker object to the Thread object
        self.workerThread.start()

    def signalExample(self, text, number):
        print(text)
        print(number)

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
This is my code so far. Any suggestions?

# -*- coding: utf-8 -*-
"""
Created on Mon Dec 03 11:12:09 2018

@author: Matthias Hochholzer
"""

import sys
import os
import subprocess
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *

class HexChecker():
    
   # def getLines():            different approche
   #     global filePath, toolPath, gotPath
   #     filePath = w.hexLine.text()
   #     toolPath = w.consolLine.text()
   #     gotPath = 1
    #    print filePath
    
    
    def checkHex(self):
        global filePath, toolPath
        filePath = w.hexLine.text()
        toolPath = w.consolLine.text()
        while go:
            global oldDate
            newDate = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(os.path.getmtime(filePath)))
            if newDate != oldDate:
                subprocess.Popen([toolPath, "-c", "SWD", "-P", filePath, "-v", "-Rst"], shell = True)
                oldDate = newDate
            time.sleep(1)
            
       
       
    def cancelFlashing(self):
        global go
        go = 0
     
     
filePath = "start"
toolPath = "start"
oldDate = "0000-00-00 00:00:00"
go = 1
hexChecker = HexChecker()

app = QApplication(sys.argv)
w = loadUi("Autoflash.ui")

w.startButton.clicked.connect(hexChecker.checkHex)
w.closeButton.clicked.connect(hexChecker.cancelFlashing)

w.show()
sys.exit(app.exec_())