Python Tkinter猜价游戏:点击确认按钮倒计时重复重置问题求助
Python猜价游戏倒计时问题修复
问题描述
- 首次点击“Valider”按钮,商品图片显示但倒计时未启动
- 输入价格提交后倒计时开始,但再次点击“Valider”提交新价格时,倒计时会重置为60秒,导致时间无限循环重置
问题根源
- 首次点击时输入框为空,
player_reponse = int(champs_de_saisie.get())触发报错,代码无法执行到倒计时启动逻辑 - 原
timer()函数用while循环+time.sleep(1)阻塞了tkinter主循环,导致界面响应异常 - 每次点击“Valider”都会重新调用倒计时启动函数,直接重置倒计时
修复后的代码
from tkinter import * # 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) # 全局变量:跟踪倒计时状态和剩余时间 timer_running = False remaining_time = 60 # 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 update_timer(): global timer_running, remaining_time if timer_running and remaining_time >= 0: timer_text["text"] = str(remaining_time) if remaining_time == 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) timer_running = False remaining_time -= 1 # 1秒后再次调用自身,实现倒计时 window.after(1000, update_timer) def game(): global timer_running, remaining_time 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 # 处理输入为空的情况 input_text = champs_de_saisie.get().strip() if not input_text: return try: player_reponse = int(input_text) except ValueError: # 输入不是数字的提示 error_txt = Label(window, text="Veuillez entrer un nombre !", bg="#CA3C66", fg="#E8AABE", font=("Verdana", 20,"italic bold")) error_txt.place(x=150, y=250) error_txt.after(duration, error_txt.destroy) return # 显示价格提示 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) elif 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) else: 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_running = False # 猜对后停止倒计时 # 仅当倒计时未运行时启动 if not timer_running: remaining_time = 60 timer_running = True update_timer() # 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()
关键修改说明
- 替换倒计时实现:用tkinter自带的
after()方法替代while+time.sleep(),避免阻塞主循环,保证界面正常响应 - 状态控制:新增全局变量
timer_running和remaining_time,跟踪倒计时是否正在运行,防止重复启动或重置 - 输入校验:处理输入为空或非数字的情况,避免首次点击报错
- 猜对逻辑:玩家猜对价格时自动停止倒计时
内容的提问来源于stack exchange,提问作者William Locufier
相关产品推荐
相关产品推荐

