How to set a pop up time limit in python? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-25 12:41:05

问题


I want to have a time limit in order to enter the input in the following code. In other words, there should be a timer tracking the time and if it exceeds the limit, the code should print out a message like "Game over" automatically without hitting any key. it is a sort of pop-up.

def human(player, panel):
    print print_panel(panel)
    print 'Your Turn! , Hint: "23" means go to row No.2 column No.3/nYou got 1 min to  move.'
    start_time = time.time()
    end_time = start_time + 60
    while True :
        move = raw_input('> ')
        if move and check(int(move), player, panel):
            return int(move)
        else:
            if (time.time() < end_time):
                print 'Wrong move >> please try again.'
            else:
                print "Game over"
                return panel, score(BLACK, panel)
                break

the other question is almost the same but the answer is not what I am looking for. I want the code to return a message when the time is over without hitting "ENTER".


回答1:


The simplest way is to use the curses module. You'll want to set nodelay(1), and poll for input. http://docs.python.org/2/howto/curses.html#user-input



来源:https://stackoverflow.com/questions/21959456/how-to-set-a-pop-up-time-limit-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!