我正在使用python创建一个闹钟来询问数学问题。但我的问题是如果用户给出了错误的答案我想继续提问。请帮助我。我正在使用tkinter创建闹钟。但是在askyesno中,是的,并且不仅仅是执行。所以请帮助我改进代码
enter code here
from tkinter import *
from tkinter import ttk
import time
import os
import sys
from tkinter import messagebox
root = Tk()
root.title("Alarm clock")
def SubmitButton():
AlarmTime= enter1.get()
Message1()
CurrentTime = time.strftime("%H:%M")
print("the alarm time is: {}".format(AlarmTime))
while AlarmTime != CurrentTime:
CurrentTime = time.strftime("%H:%M")
time.sleep(1)
x = 0
y = 0
while AlarmTime == CurrentTime:
if x == 0:
x+=1
os.system("start alarm-music.mp3")
if y<1:
label2.config(text = "Alarm music playing.....")
messagebox.showinfo(title= 'Alarm Message', message= " {}".format(enter2.get()))
while True:
if y<1:
messagebox.askquestion("answer the question in yes or no","(9^5)-1+(4|3)=18",icon='question')
y+=1
if True:
break
else:
os.system("start alarm-music.mp3")
messagebox.askyesno("answer the question","9*71=647",icon='question')
if True:
sys.close()
else:
os.system("start alarm-music.mp3")
def Message1():
AlarmTimeLable= enter1.get()
label2.config(text="the Alarm time is Counting...")
messagebox.showinfo(title = 'Alarm clock', message = 'Alarm will Ring at {}'.format(AlarmTimeLable))
alarm1 = ttk.Frame(root)
alarm1.pack()
alarm1.config(height = 200, width = 200)
alarmtime1= ttk.Label(alarm1,text = "Enter the Alarm time :")
alarmtime1.pack()
enter1 = ttk.Entry(alarm1, width = 100)
enter1.pack()
enter1.insert(3,"Please enter 24-hr format here")
AlarmMessage= ttk.Label(alarm1, text="Alarm Message:")
AlarmMessage.pack()
enter2= ttk.Entry(alarm1, width=100)
enter2.pack()
button1= ttk.Button(alarm1, text= "submit", command=SubmitButton)
button1.pack()
label2= ttk.Label(alarm1)
label2.pack()
root.mainloop()
答案 0 :(得分:1)
此代码
if True:
break
总会打破你的周期。为避免这种情况,请尝试将对话框的结果保存到任何变量,然后验证它的值:
if y<1:
answer = messagebox.askquestion("answer the question in yes or no","(9^5)-1+(4|3)=18",icon='question')
y+=1
if answer == 'yes':
break
...
answer = messagebox.askyesno("answer the question","9*71=647",icon='question')
if answer == True:
sys.close()
else:
os.system("start alarm-music.mp3")
...