如何获得两个不同函数的结果

时间:2018-11-18 10:07:49

标签: python-3.x multiprocessing timeout messagebox

您好,我有一个看起来像这样的代码

from multiprocessing import Process 
from tkinter.messagebox import *
from time import sleep

def timerclose():
    sumtimer = 0
    while sumtimer <= 10 :
        sleep(0.1)
        sumtimer = sumtimer + 0.1
        print("sumtimer",sumtimer)
    return sumtimer


def conout():

    confirmation = askokcancel ("confirmation","are you sure ?")
    return confirmation

if __name__=='__main__':
    p1 = Process(target=timerclose)
    p1.start()
    p2 = Process(target=conout)
    p2.start()

我想创建一个带有超时的askokcancel消息框。我希望弹出消息框以询问用户是否要退出并同时启动一个计数器。 10秒钟后,如果用户不按任何键(确定或取消),我将从timerclose中获得返回值,而忽略conout函数,并继续执行其余的程序。

1 个答案:

答案 0 :(得分:0)

我通过一个过程和一个共享的价值来解决它

def conout(n):

    n.value = int(0)
    confirmation = askokcancel ("confirmation","Σε 10 sec κλείνω Οκ ?")
    if confirmation == True :
        n.value = int(1)
    else:
        n.value = int(2)


if __name__=="__main__":
    p2 = Process(target=conout, args=(num,))
    p2.start()
    timerls = 0
    while timerls <= 10:
        sleep(0.25)
        timerls += 0.25
        print("timerls",timerls)
        if num.value == 1 :
            print ("end true",num.value)
            break
        elif num.value == 2:
            print ("end false", num.value)
            break
        else:
            print ("end0", num.value)