Search code examples
pythonpython-3.xwait

How to delay a python script?


I've made a tic tac toe game which basiclly whenever a player wins,the script waits five seconds and quits itself. I searched it on google and it said I could use time.sleep(). But the problem is I don't want to use time.sleep(). The reason why is because it makes the players last move not visible. Anyone has any advice for other libraries or codes to do the same thing?

this is the code but I don't blame you if you don't understand.

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()

Solution

  • You can try time.sleep() pygame.time.wait() pyplot.pause() .after() asyncio.sleep()

    For more detail check 1st answer by 'Trooper Z' here