Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 暂停执行直到按下按钮_Python 3.x_Pyqt_Pyqt4_Wait - Fatal编程技术网

Python 3.x 暂停执行直到按下按钮

Python 3.x 暂停执行直到按下按钮,python-3.x,pyqt,pyqt4,wait,Python 3.x,Pyqt,Pyqt4,Wait,我有一个QStackedWidget。在逻辑(而不是UI)中,我试图更改页面,并在那里等待,直到按下该页面上的按钮(基本上是确定/取消)。我将UI传递给类中的函数 大概是这样的: def func1(self, window): window.stackedWidget.setCurrentIndex(4) while True: window.btn_OK.clicked.connect(self.OK_func) window.btn_Canc

我有一个QStackedWidget。在逻辑(而不是UI)中,我试图更改页面,并在那里等待,直到按下该页面上的按钮(基本上是确定/取消)。我将UI传递给类中的函数

大概是这样的:

def func1(self, window):
    window.stackedWidget.setCurrentIndex(4)
    while True:
        window.btn_OK.clicked.connect(self.OK_func)
        window.btn_Cancel.clicked.connect(self.Can_func)

def OK_func(self, window):
    do_something
    window.stackedWidget.setCurrentIndex(3)
    break

def Can_func(self, window):
    window.stackedWidget.setCurrentIndex(3)
    break

for i in range(5):
    #stuff
    func1(window)  #this is where I want to pause
    #other stuff

现在我知道我不能像那样中断函数,也不能通过connect传递窗口变量,但我希望这能充分说明我的观点

只需在和中断时删除

def func1(self, window):
    window.stackedWidget.setCurrentIndex(4)
    window.btn_OK.clicked.connect(self.OK_func)
    window.btn_Cancel.clicked.connect(self.Can_func)

def OK_func(self, window):
    # do_something
    window.stackedWidget.setCurrentIndex(3)

def Can_func(self, window):
    window.stackedWidget.setCurrentIndex(3)

一种简单的方法是在循环内处理挂起的事件(因此UI保持响应),并设置/取消设置内部标志以控制循环的启动和停止

以下演示脚本显示了此想法的基本实现:

import time
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.label = QtGui.QLabel(self)
        layout.addWidget(self.label)
        self.buttonStart = QtGui.QPushButton('Start', self)
        self.buttonStart.clicked.connect(self.handleStart)
        layout.addWidget(self.buttonStart)
        self.buttonStop = QtGui.QPushButton('Stop', self)
        self.buttonStop.clicked.connect(self.handleStop)
        layout.addWidget(self.buttonStop)
        self._running = False

    def handleStart(self):
        self.buttonStart.setDisabled(True)
        self._running = True
        while self._running:
            self.label.setText(str(time.clock()))
            QtGui.qApp.processEvents()
            time.sleep(0.05)
        self.buttonStart.setDisabled(False)

    def handleStop(self):
        self._running = False

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 200, 100)
    window.show()
    sys.exit(app.exec_())

这不会暂停执行。@Faller,应该暂停哪个部分<代码>正常功能
<代码>Can_func?其他部分?在func1过程中,在“setCurrentIndex”之后应该暂停,直到它从OK_func或Can获得输入_func@Faller,如果存在无限循环,
OK_func
Can_func
永远不能被调用。和回调将被注册多次。更重要的是,它冻结了GUI。@Faller,在
while
循环之后有什么东西吗?这太棒了。绝对是我想要的,非常感谢。