利用tkinter做gui界面,测试时间为0,逻辑看着是跟我正常代码一样的,但还是没算出来,求解答
import tkinter as tk
import random
import datetime
import time
window = tk.Tk()
score = 0
count = 1
a = random.randint(1, 9)
b = random.randint(1, 9)
mul01 = tk.StringVar()
mul01.set(str(a))
mul02 = tk.StringVar()
mul02.set(str(b))
count_title = tk.StringVar()
count_title.set('第1道题')
score_lab = tk.StringVar()
score_lab.set('0')
answer_lab = tk.StringVar()
gameove = tk.StringVar()
t=tk.StringVar()
time_solve = tk.DoubleVar()
gameove.set("共测试10道乘法,每题10分,总分100分")
window.title("乘法口诀测试")
window.geometry('600x500')
window["background"] = "LightCyan"
title01 = tk.Label(window,
textvariable=gameove,
font=('华文行楷', 15),
bg='SpringGreen')
title01.place(x=120, y=30)
label01 = tk.Label(window,
textvariable=count_title,
font=('华文行楷', 20),
bg='hotpink') # 第N道题
label01.place(x=100, y=100)
title02 = tk.Label(window, text="请输入答案:", font=('华文行楷', 20),
bg='hotpink')
title02.place(x=80, y=250)
title03 = tk.Label(window,
textvariable=score_lab,
font=('华文行楷', 28),
bg='hotpink') # 分数lab
title03.place(x=340, y=400)
title04 = tk.Label(window, text="得分:", font=('华文行楷', 28), bg='hotpink') # 得分lab
title04.place(x=190, y=400)
title05 = tk.Label(window, textvariable=answer_lab, font=('华文行楷', 28), bg='hotpink') # 得分lab
title05.place(x=220, y=450)
entry01 = tk.Entry(window, textvariable=0, width=10, font=('华文行楷', 20), show=None) # 文本输入框
entry01.place(x=300, y=250)
label02 = tk.Label(window, textvariable=mul01, font=('华文行楷', 28),
bg='hotpink') # 被乘数lab
label02.place(x=180, y=175)
label03 = tk.Label(window, textvariable=mul02, font=('华文行楷', 28),
bg='hotpink') # 乘数lab
label03.place(x=340, y=175)
imgLabel = tk.Label(window, text='X', font=('黑体', 28),
bg='hotpink') # 乘号标签
imgLabel.place(x=263, y=170)
def surecallback():
global a, b, count,startTime,endTime
if count <= 10:
product = a * b
startTime = time.time()
result = int(entry01.get())
if result == product:
global score
score = score + 10
score_lab.set(str(score))
answer_lab.set("答对了")
else:
answer_lab.set("答错了")
endTime = time.time()
entry01.delete(0, tk.END)
a = random.randint(1, 9)
b = random.randint(1, 9)
mul01.set(str(a))
mul02.set(str(b))
count = count + 1
c = '第' + str(count) + '道题'
count_title.set(c)
else:
title01.destroy()
title02.destroy()
entry01.destroy()
title03.destroy()
title04.destroy()
title05.destroy()
label01.destroy()
label02.destroy()
label03.destroy()
imgLabel.destroy()
button01.destroy()
gameove.set("测试结束")
title06 = tk.Label(window,
textvariable=gameove, font=('华文行楷', 40),
bg='SpringGreen')
title06.place(x=220, y=30)
t.set('进行本次乘法测试花费的时间为:%s' % (endTime - startTime))
# t1 = '进行每次测试花费的平均时间为:%s'%(time_solve/10.0)
title07 = tk.Label(window,
textvariable=t, font=('华文行楷', 15),
bg='SpringGreen')
title07.place(x=30, y=100)
button01 = tk.Button(text='确认',
font=('华文行楷', 20),
width=8,
height=1,
command=surecallback)
button01.place(x=230, y=310)
window.mainloop()
用time.clock函数试试吧
给你找了一篇非常好的博客,你可以看看是否有帮助,链接:Tkinter实现一个简单的GUI界面