-1

我对一些 Python 代码有一些问题,这取决于我的逻辑。我有一个通过 UI 中的按钮单击调用的主函数,在此函数中取决于按钮单击的结果,将执行几个语句,我遇到的问题是分支到函数,然后返回到这一点执行下一个函数调用。这是我的代码:

                return #This is where I want this function to terminate and then return to the function above to execute the nextFunctionCall() function.

将不胜感激任何帮助,谢谢。我在这里遇到的问题是每个函数运行一次,第一个函数应该执行 30 次,然后执行 else 子句,前提是 elif 语句中更改的状态没有发生。

4

1 回答 1

1

如果你想nextFunctionCall(root)在 30 次调用后执行alarmActive,你不需要让自己复杂化。只需将其删除并在块enableCode中调用它:else

def alarmActive(root, period=0):
    # ...
    else:
        nextFunctionCall(root)

但是,可以减少很多冗余代码。例如,如果您只想Flash从 True 或 False 进行设置,反之亦然,并根据值打印"On"和,这更短更直接:"Off"

def alarmActive(root, period=0):
    global Flash
    if period <30 and AlarmStatus == "On":
        Flash = not Flash
        print("On" if Flash else "Off")
        period += 1
        print(period)
        root.after(500, lambda: alarmActive(root, period))
    else:
        if (AlarmStatus == "Off"):
            print("Alarm has been disabled before activation")
        nextFunctionCall(root)
于 2013-04-12T19:54:43.823 回答