I started programming in python recently. My program follows the rules of the game of fortune: the player clicks on the button "demarrer", the rules appear, the player clicks on the button "valider", an image appears and the player gives a price by entering it in the input field and clicks on "valider". If the price is higher, the computer indicates it and vice versa. There is also a 60s countdown. This is where the problem occurs: when I click on "valider", for the first time, the image appears but the countdown does not start. I enter a value and click confirm and the countdown starts. Since I have very little chance of finding the right value right away, I'm trying my luck again. So I enter a new number and the countdown returns to its starting value (60 s), so the time is infinite and regenerates each time I click on "valider". I don't know if I was very clear but if anyone could help me I would be happy to accept. Thanks in advance. Here is my script:
from tkinter import *
import time
# Window -------------------------------------------------------------------------------------- -------------------------
window = Tk()
window.minsize(800, 700)
window.maxsize(800, 700)
window.title("Le juste prix")
img_fond = PhotoImage(file="Juste prix redim.png")
image = Label(window, image=img_fond)
image.place(x=0, y=0)
# Def ------------------------------------------------------------------------------------------------------------------
def starting():
text_1 = Label(window, text="Des objets vont apparaître. Donne un prix le plus proche \n possible dans le temps imparti. Clique sur valider pour commencer.", font=("Verdana", 15, "italic bold"),
bg="grey", fg="white")
text_1.place(x=15, y=100)
def timer(duree):
while duree >= 0:
time.sleep(1)
timer_text["text"] = str(duree)
window.update()
if duree == 0:
text_temps = Label(window, text="Tu n'as plus de temps !", fg="white", bg="#CA3C66", font="Verdana 20")
text_temps.place(x=450, y=300)
duree -= 1
def game():
price = 96650
duration = 3000
img_1 = PhotoImage(file="x6 redim.png")
x6 = Label(window, image=img_1)
x6.place(x=100, y=350)
x6.image = img_1
player_reponse = int(champs_de_saisie.get())
if player_reponse > price:
less_txt_img1 = Label(window, text="Moins cher !", bg="#CA3C66", fg="#E8AABE" , font=("Verdana", 20,"italic bold"))
less_txt_img1.place(x=150, y=250)
less_txt_img1.after(duration, less_txt_img1.destroy)
if player_reponse < price:
more_txt_img1 = Label(window, text="Plus cher !", bg="#CA3C66", fg="#E8AABE" , font=("Verdana", 20, "italic bold"))
more_txt_img1.place(x=150, y=250)
more_txt_img1.after(duration, more_txt_img1.destroy)
if player_reponse == price:
win_txt_img1 = Label(window, text="Bravo tu as trouvé !", bg="#CA3C66", fg="#E8AABE" , font=("Verdana", 20, "italic bold"))
win_txt_img1.place(x=100, y=250)
timer(60)
# Widgets --------------------------------------------------------------------------------------------------------------
champs_de_saisie = Entry(window, width=15, font="Arial 15")
champs_de_saisie.place(x=525, y=450)
text_2 = Label(window, text="€", font="Verdana 15", bg="grey", fg="white")
text_2.place(x=675, y=449)
text_3 = Label(window, text="Ton prix :", font="Verdana 15", bg="grey", fg="white")
text_3.place(x=550, y=400)
timer_text = Label(window, text="", bg="#F4CFDF", fg="#A7001E", width=7, font="Arial 30")
timer_text.place(x=525, y=200)
start_button = Button(window, text="Commencer", bg="#212E53", fg="white", font="Arial 15", width=36, command=starting)
start_button.place(x=0, y=0)
valid_button = Button(window, text="Valider", bg="#7AA95C", fg="#e5e7e6", width=10, font="Arial 15", command=game)
valid_button.place(x=550, y=500)
leave_button = Button(window, text="Quitter", bg="#A7001E", fg="white", font="Arial 15", width=36, command=window.destroy)
leave_button.place(x=400, y=0)
window.mainloop()
Something like this should do it:
onetime = False
def starting():
global onetime
onetime = False
# other parts of this function
def game():
# other parts of game ...
global onetime
if onetime == False:
onetime = True
timer(60)
# remainder of the program
So you can then call game
multiple times without resetting the timer.