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

标签 python pyqt

按“确定”按钮将运行 while_loop() 方法,该方法会在屏幕上打印一些消息。

while_loop() 方法运行时,主对话框变得不负责任。 有趣的是,即使对话框窗口关闭后,函数本身仍然会运行。显然这不是我们所希望的。当对话框关闭时,while_loop()方法也应该停止。如果运行 while_loop() 方法也不会锁定(变得不负责任)主对话框窗口,那就太好了...

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_())

最佳答案

您可能想使用线程来解决这个问题。这样你的线程就会关闭

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)

关于Python PyQt : How Run While Loop Without Locking Main Dialog Window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340230/

相关文章:

python - python中的束和字典类型有什么区别?

python - rPython 和 __future__ 导入

qt - 如何动态创建Q_Properties

python - PyQt 代码拆分——设计与功能

python - 使用 QFileSystemModel 扩展 QTreeView 中的项目

python - 使用 Qt-GUI 的 Python 程序中的简单文件浏览器/文件选择器?

python - 将列名转换为数字?

python - Pandas 迭代行并从另一列中删除一列中的字符串值

python - 每当有来自串行端口 python 3.x 的新数据时,从串行数据更新 tkinter 标签

python - 子进程 Popen 阻塞 PyQt GUI