来做几道算术题
学生时代最喜欢做算术题,寒暑假一天就能做完所有算术作业。
走上社会后充分利用每次超市大采购的机会,默算总价对账单。
特意用python开发了一个小应用程序,随时可以做几道算术题~~
import random
import Tkinter as tk
from tkMessageBox import *
import datetime
starttime = datetime.datetime.now()
window=tk.Tk()
window.title('来做几道算术题')
window.geometry('800x600')
for i in range(9):
w1_i = random.randint(0, 99)
w2_i = random.randint(0, 99)
v1_i = tk.StringVar()
v2_i = tk.StringVar()
v3_i = tk.StringVar()
v4_i = tk.StringVar()
tk.Label(window,textvariable=v1_i,width=5).grid(row = 5+i, column = 1)
v1_i.set(w1_i)
tk.Label(window,textvariable=v2_i,width=5).grid(row = 5+i, column = 2)
v2_i.set("+")
tk.Label(window,textvariable=v3_i,width=5).grid(row = 5+i, column = 3)
v3_i.set(w2_i)
tk.Label(window,textvariable=v4_i,width=5).grid(row = 5+i, column = 4)
v4_i.set("=")
v_i= tk.StringVar()
tk.Entry(window,textvariable=v_i,width=10).grid(row = 5+i, column = 5)
def log():
for i in range(9):
flag=0
global w1_i,w2_i,num_i
w_i= v_i.get() #此处是关键,一定要有点击触发,v的值才会从空更新为键盘输入值
if int(w_i)<>int(w1_i+w2_i) :
flag=1
if flag==0:
endtime = datetime.datetime.now()
usetime=(endtime - starttime).seconds
showinfo(title='Hi', message='本组训练结束,用时%dS!请继续工作!'%usetime)
window.destroy()
if flag==1:
showerror(title='Hi', message='出错了!')
tk.Button(window,text="submit",command=log).grid(row = 20, column = 10)
window.mainloop()