在Python中暂停循环

2024-07-08 11:38:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个循环,它根据用户输入的数字使用PySide创建windows 每个窗口都会调用一些其他函数。
我希望在完成属于第一个窗口的所有命令后打开第二个窗口。
那么,Python中有没有一种方法可以告诉循环停止,直到某个标志为真

这就是我要做的

for i in range(values):
    self.CreatWindow()      # the function that creates the window



def CreatWindow(self):
    window = QtGui.QMainWindow(self)
    window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    combo = QtGui.QComboBox(window)
    combo.addItem(" ")
    combo.addItem("60")
    combo.addItem("45")
    combo.activated[str].connect(self.onActivated)  

    btn = QtGui.QPushButton('OK', window)
    btn.clicked.connect(self.ComputeVec)
    window.show()

def onActivated(self, text):
    angle = int(text)

def ComputeVec(self):
    window.close()
    getVecValue(angle)

现在在这个函数中,窗口有一些对其他函数的调用,我想在最后一个函数getVecValue中将标志设置为True,该函数将执行一些计算并存储结果。在


Tags: the函数textself标志defconnectwindow
2条回答

您可以在ComputeVec中调用createwindow,而不是使用不同的循环来打开新窗口 并使用全局变量count来维护之前创建的窗口的计数。在

count = 0
def ComputeVec(self):
    window.close()
    getVecValue(angle)
    global count
    count += 1
    if count in range(values) : 
        self.CreatWindow()

由于函数调用self.CreateWindow等待被调用函数的返回值,所以循环的行为已经像这样了。在

您可以从self.CreateWindow返回一个适当的值,例如return True,然后执行以下操作:

for i in range(values):
    success = self.CreateWindow()
    if success:
        continue

不管怎样,如果self.CreateWindow中没有返回值,语句self.CreateWindow()仍然被计算并得到None。直到达到这个结果,循环才会结束。在

相关问题 更多 >

    热门问题