alphanumeric
alphanumeric

Reputation: 19369

Python PyQt: How Run While Loop Without Locking Main Dialog Window

Pressing Ok button will run while_loop() method which prints some message on a screen.

While while_loop() method is running the main dialog becomes irresponsible. It's interesting that even after a dialog window is closed the function itself will be still running. Apparently that is not desired. When the dialog box is closed while_loop() method should be stopped as well. It would be great if running a while_loop() method wouldn't lock (make irresponsible) the main dialog window as well...

import sys, time
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.main_layout = QtGui.QVBoxLayout()

        ok_button = QtGui.QPushButton("Run")
        ok_button.clicked.connect(self.OK)      
        self.main_layout.addWidget(ok_button)       

        cancel_button = QtGui.QPushButton("Cancel")
        cancel_button.clicked.connect(self.cancel)      
        self.main_layout.addWidget(cancel_button)

        central_widget = QtGui.QWidget()
        central_widget.setLayout(self.main_layout)
        self.setCentralWidget(central_widget)

    def myEvenListener(self):
        state=True
        while state:
            for i in range(10,100):
                time.sleep(i*0.01)
                print '.'*i

    def OK(self):
        self.myEvenListener()       

    def cancel(self):
        sys.exit()  

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.resize(480, 320)
    window.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 6503

Answers (1)

thecreator232
thecreator232

Reputation: 2185

You might want to use thread to solve this problem. and so that your thread closes

import threading
import sys, time
from PyQt4 import QtCore, QtGui
import psutil
import os

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.main_layout = QtGui.QVBoxLayout()

        ok_button = QtGui.QPushButton("Run")
        ok_button.clicked.connect(self.OK)      
        self.main_layout.addWidget(ok_button)       

        cancel_button = QtGui.QPushButton("Cancel")
        cancel_button.clicked.connect(self.cancel)      
        self.main_layout.addWidget(cancel_button)

        central_widget = QtGui.QWidget()
        central_widget.setLayout(self.main_layout)
        self.setCentralWidget(central_widget)

    def myEvenListener(self,stop_event):
        state=True
        while state and not stop_event.isSet():
            for i in range(10,100):
                time.sleep(i*0.01)
                print '.'*i

    def OK(self):
        self.stop_event=threading.Event()
        self.c_thread=threading.Thread(target=self.myEvenListener, args=(self.stop_event,))
        self.c_thread.start()       

    def cancel(self):
        self.stop_event.set()
        self.close()    

def main():
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.resize(480, 320)
    window.show()
    app.exec_()
main()


def kill_proc_tree(pid, including_parent=True):    
    parent = psutil.Process(pid)
    if including_parent:
        parent.kill()

me = os.getpid()
kill_proc_tree(me)

Upvotes: 2

Related Questions