如何运行“While Loop"用 PyQt5? [英] How to run a "While Loop" with PyQt5?

查看:78
本文介绍了如何运行“While Loop"用 PyQt5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前段时间制作了一个脚本来记录用户的监控帧,现在我正在尝试为它创建一个 GUI.目前它只有一个 START 和一个 STOP 按钮,但 STOP 按钮不会停止录制.

I made one script some time ago to record the monitor frames of the user and now I am trying to create a GUI for it. For now it has only a START and a STOP button, but the STOP button does not stop the recording.

如何更改我的 stop_thread 函数以使其正常工作?我应该先终止工人然后终止线程吗?我怎样才能终止工作人员?

How could I change my stop_thread function for it to work? Should I terminate the worker first and then the thread? How can I terminate the worker anyway?

import sys
from PyQt5.QtWidgets import (QWidget,
                             QPushButton, QApplication, QGridLayout)
from PyQt5.QtCore import QThread, QObject


class Worker(QObject):

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)

    def do_work(self):
        i = 1
        while True:
            print(i)
            QThread.sleep(1)
            i = i + 1

    def stop(self):
        print("stopped")
        self.deleteLater() # How do I stop it?


class Gui(QWidget):

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

    def initUI(self):

        # Buttons:
        self.btn_start = QPushButton('Start')
        self.btn_start.resize(self.btn_start.sizeHint())
        self.btn_start.move(50, 50)
        self.btn_stop = QPushButton('Stop')
        self.btn_stop.resize(self.btn_stop.sizeHint())
        self.btn_stop.move(150, 50)

        # GUI title, size, etc...
        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('ThreadTest')
        self.layout = QGridLayout()
        self.layout.addWidget(self.btn_start, 0, 0)
        self.layout.addWidget(self.btn_stop, 0, 50)
        self.setLayout(self.layout)

        # Thread:
        self.thread = QThread()
        self.worker = Worker()
        self.worker.moveToThread(self.thread)

        self.thread.started.connect(self.worker.do_work) # when thread starts, start worker
        self.thread.finished.connect(self.worker.stop) # when thread finishes, stop worker

        # Start Button action:
        self.btn_start.clicked.connect(self.thread.start)

        # Stop Button action:
        self.btn_stop.clicked.connect(self.stop_thread)

        self.show()

    # When stop_btn is clicked this runs. Terminates the worker and the thread.
    def stop_thread(self):
        print("It should stop printing numbers now and not crash")
        self.worker.disconnect()
        self.thread.terminate()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = Gui()
    sys.exit(app.exec_())

虽然 MalloyDelacroix answer 工作了一段时间,但我现在遇到了一个问题:

While MalloyDelacroix answer worked for some time, I've now run into a problem:

如果来自 Workerdo_work 是一个永远循环的导入函数,我如何通过单击按钮来停止它?我想强制关闭它.

If do_work from Worker is an imported function which loops forever, how can I stop it with a button click? I want to force close it.

from test import s_main

class Worker(QObject):

    finished = pyqtSignal()  # give worker class a finished signal

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)
        self.continue_run = True  # provide a bool run condition for the class

    def do_work(self):

        s_main()

        self.finished.emit()  # emit the finished signal when the loop is done

    def stop(self):
        self.continue_run = False  # set the run condition to false on stop

test.py:

def s_main():
    i = 1
    while True:
        i = i + 1
        print(i)

推荐答案

以下是您发布的代码的工作示例.我已经在我改变的领域发表了评论,并解释了原因.

Below is a working example of the code you posted. I have made comments in the areas that I changed with the explanations as to why.

import sys
from PyQt5.QtWidgets import (QWidget,
                         QPushButton, QApplication, QGridLayout)
from PyQt5.QtCore import QThread, QObject, pyqtSignal


class Worker(QObject):

    finished = pyqtSignal()  # give worker class a finished signal

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)
        self.continue_run = True  # provide a bool run condition for the class

    def do_work(self):
        i = 1
        while self.continue_run:  # give the loop a stoppable condition
            print(i)
            QThread.sleep(1)
            i = i + 1
        self.finished.emit()  # emit the finished signal when the loop is done

    def stop(self):
        self.continue_run = False  # set the run condition to false on stop


class Gui(QWidget):

    stop_signal = pyqtSignal()  # make a stop signal to communicate with the worker in another thread

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

    def initUI(self):

        # Buttons:
        self.btn_start = QPushButton('Start')
        self.btn_start.resize(self.btn_start.sizeHint())
        self.btn_start.move(50, 50)
        self.btn_stop = QPushButton('Stop')
        self.btn_stop.resize(self.btn_stop.sizeHint())
        self.btn_stop.move(150, 50)

        # GUI title, size, etc...
        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('ThreadTest')
        self.layout = QGridLayout()
        self.layout.addWidget(self.btn_start, 0, 0)
        self.layout.addWidget(self.btn_stop, 0, 50)
        self.setLayout(self.layout)

        # Thread:
        self.thread = QThread()
        self.worker = Worker()
        self.stop_signal.connect(self.worker.stop)  # connect stop signal to worker stop method
        self.worker.moveToThread(self.thread)

        self.worker.finished.connect(self.thread.quit)  # connect the workers finished signal to stop thread
        self.worker.finished.connect(self.worker.deleteLater)  # connect the workers finished signal to clean up worker
        self.thread.finished.connect(self.thread.deleteLater)  # connect threads finished signal to clean up thread

        self.thread.started.connect(self.worker.do_work)
        self.thread.finished.connect(self.worker.stop)

        # Start Button action:
        self.btn_start.clicked.connect(self.thread.start)

        # Stop Button action:
        self.btn_stop.clicked.connect(self.stop_thread)

        self.show()

    # When stop_btn is clicked this runs. Terminates the worker and the thread.
    def stop_thread(self):
        self.stop_signal.emit()  # emit the finished signal on stop


if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = Gui()
    sys.exit(app.exec_())

答案还是基本一样,你只需要将停止按钮连接到其他方法运行条件即可.

The answer is still basically the same, you just need to connect the stop button to the other methods run condition.

test.py

import time

run = True

def s_main():
    x = 1
    while run:
        print(x)
        x += 1
        time.sleep(1)

worker.py

import test

class Worker(QObject):

    finished = pyqtSignal()  # give worker class a finished signal

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)

    def do_work(self):

        s_main()

        self.finished.emit()  # emit the finished signal when the loop is done

    def stop(self):
        test.run = False  # set the run condition to false on stop    

这篇关于如何运行“While Loop"用 PyQt5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆