Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何经营一家公司;“While循环”;用PyQt5?_Python_Pyqt5 - Fatal编程技术网

Python 如何经营一家公司;“While循环”;用PyQt5?

Python 如何经营一家公司;“While循环”;用PyQt5?,python,pyqt5,Python,Pyqt5,不久前我编写了一个脚本来记录用户的监视器帧,现在我正在尝试为它创建一个GUI。目前,它只有一个开始和停止按钮,但停止按钮并不停止录音 如何更改我的stop\u线程函数使其工作?我应该先终止辅助线程,然后再终止线程吗?我怎样才能解雇这个工人呢 import sys from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QGridLayout) from PyQt5.

不久前我编写了一个脚本来记录用户的监视器帧,现在我正在尝试为它创建一个GUI。目前,它只有一个开始和停止按钮,但停止按钮并不停止录音

如何更改我的
stop\u线程
函数使其工作?我应该先终止辅助线程,然后再终止线程吗?我怎样才能解雇这个工人呢

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_())
编辑:工作了一段时间后,我遇到了一个问题:

如果
dou_work
from
Worker
是一个永久循环的导入函数,我如何通过单击按钮停止它?我想强行关闭它

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:


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

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_())
编辑:

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

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    

非常感谢你!我结束了将线程和工作线程创建代码移动到一个函数的过程(就像我使用stop_Thread函数那样),这样我可以不断地停止并重新启动进程。您好,我再次遇到了一个意外问题,您能看到我更新的问题(编辑部分)吗?我编辑了我的答案以解决您的更新问题。将来,这类问题可能更适合新问题,而不是编辑已回答的旧问题。
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