如何在python中循环2个函数?

2024-03-28 21:39:36 发布

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

我有两个功能,我需要调用对方基于用户设置。也就是说,如果用户打开了repeat,它只需要继续运行,直到手动停止,我有一个按钮。如何才能做到这一点,而不造成一个无限循环错误崩溃的程序?你知道吗

def T1_Timer(list):
    msg = list[0]
    global t1
    T1_List = list
    t1 = threading.Timer(MultiTimer2Settings.T1_Time, msg)
    while t1.is_alive():
        if not t1.is_alive():
            return
        else:
            time.sleep(1)
    Parent.SendTwitchMessage(msg)
    T1_List.pop(0)
    return T1_List

def DoRun1():
    T1_List1 = []
    T1_List2 = []
    while not StopPressed:
        T1_List1 = CheckList1(T1_List2)
        T1_List2 = T1_Timer(T1_List1)
    return
StopPressed = False
def StopButton():
    global StopPressed
    StopPressed = True
    return
def CheckList1(T1_List=[]):
    global t1
    t1 = threading.Timer(MultiTimer2Settings.T1_Time, "")
    if not t1.is_alive() and len(T1_List) <= 0:
        if MultiTimer2Settings.T1M1_Enabled:
            T1_List.append(MultiTimer2Settings.T1M1)
        if MultiTimer2Settings.T1M2_Enabled:
            T1_List.append(MultiTimer2Settings.T1M2)
        if MultiTimer2Settings.T1M3_Enabled:
            T1_List.append(MultiTimer2Settings.T1M3)
        if MultiTimer2Settings.T1M4_Enabled:
            T1_List.append(MultiTimer2Settings.T1M4)
        if MultiTimer2Settings.T1M5_Enabled:
            T1_List.append(MultiTimer2Settings.T1M5)
    return T1_List

Tags: returnifisdefenabledmsggloballist
2条回答

只需使用while循环:

stopButtonPressed = False
var1 = <initial value>
while not stopButtonPressed:
    var2 = function1(var1)
    var1 = function2(var2)

它们应该返回传递给另一个函数的值,而不是每个函数调用另一个函数。然后可以将这些变量放入变量中,并在后续调用中传递它们。你知道吗

像这样的

Break = 0

Function1():
    test = 12
    print test
    return

Function1():
    test1 = 13
    print test2
    return

while Break < 15:
    Break += 1
    Function1()
    Function2()

使用一个函数调用另一个函数将不在循环中,因此这样做是最简单的选择

试着运行它!你知道吗

相关问题 更多 >