如何延迟python脚本? [关闭]

Posted

技术标签:

【中文标题】如何延迟python脚本? [关闭]【英文标题】:How to delay a python script? [closed] 【发布时间】:2021-10-13 12:59:47 【问题描述】:

我制作了一个井字游戏,基本上每当玩家获胜时,脚本都会等待五秒钟并自行退出。 我在谷歌上搜索它说我可以使用time.sleep()。 但问题是我不想使用time.sleep()。 原因是因为它使玩家的最后一步不可见。 有人对其他库或代码做同样的事情有什么建议吗?

这是代码,但如果你不明白,我不怪你。

from tkinter import *
import time
root = Tk()

howmanygone = 0
winningcondition1x = False
winningcondition2x = False
winningcondition3x = False
winningcondition4x = False
winningcondition5x = False
winningcondition6x = False
winningcondition7x = False
winningcondition8x = False
winningcondition9x = False
winningcondition1o = False
winningcondition2o = False
winningcondition3o = False
winningcondition4o = False
winningcondition5o = False
winningcondition6o = False
winningcondition7o = False
winningcondition8o = False
winningcondition9o = False
def destroy() :
    time.sleep(5)
    root.quit()
turn = ["x","o","x","o","x","o","x","o","x"]
indexcounter = 0
def one() :
    global winningcondition1x
    global winningcondition1o
    global turn
    global indexcounter
    global howmanygone
    button1.config(text=turn[indexcounter] , state = "disabled")
    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition1x = True
    else :
        winningcondition1o = True
    indexcounter += 1
    winningcondition1x = True
    print(winningcondition1x)
    if howmanygone >= 3 and winningcondition1x == True and winningcondition5x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1x == True and winningcondition2x == True and winningcondition3x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1x == True and winningcondition4x == True and winningcondition7x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition5o == True and winningcondition9o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition2o == True and winningcondition3o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition4o == True and winningcondition7o == True :
        print("O Won")
        destroy()

def two() :
    global winningcondition2x
    global winningcondition2o
    global turn
    global indexcounter
    global howmanygone
    button2.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition2x = True
    else :
        winningcondition2o = True
    indexcounter += 1
    print(winningcondition2x)
    if howmanygone >= 3 and winningcondition1x == True and winningcondition2x == True and winningcondition3x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition2x == True and winningcondition5x == True and winningcondition8x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition2o == True and winningcondition3o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition2o == True and winningcondition5o == True and winningcondition8o == True :
        print("O Won")
        destroy()
def three() :
    global winningcondition3x
    global winningcondition3o
    global turn
    global indexcounter
    global howmanygone
    button3.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    winningcondition3x = True
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition3x = True
    else :
        winningcondition3o = True
    indexcounter += 1
    if howmanygone >= 3 and winningcondition3x == True and winningcondition5x == True and winningcondition7x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1x == True and winningcondition2x == True and winningcondition3x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition3x == True and winningcondition6x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition3o == True and winningcondition5o == True and winningcondition7o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition2o == True and winningcondition3o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition3o == True and winningcondition6o == True and winningcondition9o == True :
        print("O Won")
        destroy()
def four() :
    global winningcondition4x
    global winningcondition4o
    global turn
    global indexcounter
    global howmanygone
    button4.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition4x = True
    else :
        winningcondition4o = True
    indexcounter += 1
    print(winningcondition4x)
    if howmanygone >= 3 and winningcondition1x == True and winningcondition4x == True and winningcondition7x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition4x == True and winningcondition5x == True and winningcondition6x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition4o == True and winningcondition7o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition4o == True and winningcondition5o == True and winningcondition6o == True :
        print("O Won")
        destroy()

def five() :
    global winningcondition5x
    global winningcondition5o
    global turn
    global indexcounter
    global howmanygone
    button5.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition5x = True
    else :
        winningcondition5o = True
    indexcounter += 1
    print(winningcondition5x)
    if howmanygone >= 3 and winningcondition1x == True and winningcondition5x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    elif howmanygone >= 3 and winningcondition3x == True and winningcondition5x == True and winningcondition7x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition2x == True and winningcondition5x == True and winningcondition8x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition4x == True and winningcondition5x == True and winningcondition6x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition5o == True and winningcondition9o == True :
        print("O Won")
        destroy()
    elif howmanygone >= 3 and winningcondition3o == True and winningcondition5o == True and winningcondition7o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition2o == True and winningcondition5o == True and winningcondition8o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition4o == True and winningcondition5o == True and winningcondition6o == True :
        print("O Won")
        destroy()
def six() :
    global winningcondition6x
    global winningcondition6o
    global turn
    global indexcounter
    global howmanygone
    button6.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition6x = True
    else :
        winningcondition6o = True
    indexcounter += 1
    print(winningcondition6x)
    if howmanygone >= 3 and winningcondition3x == True and winningcondition6x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition4x == True and winningcondition5x == True and winningcondition6x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition3o == True and winningcondition6o == True and winningcondition9o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition4o == True and winningcondition5o == True and winningcondition6o == True :
        print("O Won")
        destroy()
def seven() :
    global winningcondition7x
    global winningcondition7o
    global turn
    global indexcounter
    global howmanygone
    button7.config(text=turn[indexcounter], state = "disabled")
    indexcounter += 1

    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition7x = True
    else :
        winningcondition7o = True
    howmanygone += 1
    print(winningcondition7x)
    if howmanygone >= 3 and winningcondition3x == True and winningcondition5x == True and winningcondition7x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1x == True and winningcondition4x == True and winningcondition7x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition7x == True and winningcondition8x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition3o == True and winningcondition5o == True and winningcondition7o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition4o == True and winningcondition7o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition7o == True and winningcondition8o == True and winningcondition9o == True :
        print("O Won")
        destroy()
def eight() :
    global winningcondition8x
    global winningcondition8o
    global turn
    global indexcounter
    global howmanygone
    button8.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition8x = True
    else :
        winningcondition8o = True
    indexcounter += 1
    print(winningcondition8x)
    if howmanygone >= 3 and winningcondition7x == True and winningcondition8x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition2x == True and winningcondition5x == True and winningcondition8x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition7o == True and winningcondition8o == True and winningcondition9o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition2o == True and winningcondition5o == True and winningcondition8o == True :
        print("O Won")
        destroy()

def nine() :
    global winningcondition9x
    global winningcondition9o
    global turn
    global indexcounter
    global howmanygone
    button9.config(text=turn[indexcounter], state = "disabled")

    howmanygone += 1
    if indexcounter == 0 or indexcounter == 2 or indexcounter == 4 or indexcounter == 6 or indexcounter == 8 :
        winningcondition9x = True
    else :
        winningcondition9o = True
    indexcounter += 1
    print(winningcondition9x)
    if howmanygone >= 3 and winningcondition1x == True and winningcondition5x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition7x == True and winningcondition8x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition3x == True and winningcondition6x == True and winningcondition9x == True :
        print("X Won")
        destroy()
    if howmanygone >= 3 and winningcondition1o == True and winningcondition5o == True and winningcondition9o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition7o == True and winningcondition8o == True and winningcondition9o == True :
        print("O Won")
        destroy()
    if howmanygone >= 3 and winningcondition3o == True and winningcondition6o == True and winningcondition9o == True :
        print("O Won")
        destroy()




button1 = Button(root,text=" ",command = one,padx = 15,pady = 10)
button1.grid(row = 0,column = 0)
button2 = Button(root,text=" ",command = two,padx = 15,pady = 10)
button2.grid(row = 0,column = 1)
button3 = Button(root,text=" ",command = three,padx = 15,pady = 10)
button3.grid(row = 0,column = 2)
button4 = Button(root,text=" ",command = four,padx = 15,pady = 10)
button4.grid(row = 1,column = 0)
button5 = Button(root,text=" ",command = five,padx = 15,pady = 10)
button5.grid(row = 1,column = 1)
button6 = Button(root,text=" ",command = six,padx = 15,pady = 10)
button6.grid(row = 1,column = 2)
button7 = Button(root,text=" ",command = seven,padx = 15,pady = 10)
button7.grid(row = 2,column = 0)
button8 = Button(root,text=" ",command = eight,padx = 15,pady = 10)
button8.grid(row = 2,column = 1)
button9 = Button(root,text=" ",command = nine,padx = 15,pady = 10)
button9.grid(row = 2,column = 2)




mainloop()

【问题讨论】:

为什么它会让玩家的最后一步不可见。请发布代码或截图,以便我们了解您尝试将 time.sleep() 放在哪里 请阅读listsloops / control flow。 【参考方案1】:

你可以试试time.sleep()pygame.time.wait()pyplot.pause().after()asyncio.sleep()

有关更多详细信息,请查看“Trooper Z”here 的第一个答案

【讨论】:

以上是关于如何延迟python脚本? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Python 时间延迟

如何在Python中进行时间延迟[重复]

如何将 bash 脚本代码更改为 Python [关闭]

如何在另一个脚本中运行 Python 脚本并立即关闭自身

如何在 GPU 上启用 php 运行 python 脚本? [关闭]

python 延迟脚本ARGS-V1