import tkinter as tk
import tkinter.messagebox
import random
try:
import easygui
except:
tk.messagebox.showerror("无法导入easygui模块!")
win=tk.Tk()
win.title("计算题")
win.geometry("270x130")
win.resizable(width=False, height=False)
var_num=tk.StringVar()
var_num.set('')
var_type=tk.StringVar()
var_type.set('')
def fuhao():
pass
def ques(a,b):
if a=="1":
fuhao= "+"
elif a=="2":
fuhao= "-"
elif a=="3":
fuhao= "×"
else:
fuhao= "÷"
num=b
_type=a
score=0
for i in range(num):
a=random.randint(0,100)
b=random.randint(0,100)
y_ans=int(easygui.enterbox((a,fuhao,b),"计算题"))
if fuhao=="+":
r_ans=a+b
elif fuhao=="-":
r_answer=a-b
elif fuhao=="×":
r_answer=a*b
else:
r_answer=a/b
if y_ans==r_ans:
print("Right")
score+=100/num
else:
print("Wrong")
tk.messagebox.showinfo("得分",score)
def start():
ques_num=var_num.get()
_type_num=var_type.get()
if _type_num in ["1","2","3","4"]:
ques_num=int(ques_num)
ques(_type_num,ques_num)
elif _type_num=="":
tk.messagebox.showinfo("计算题",'输入的类型为空,如需帮助,请按"类型"按钮')
else:
tk.messagebox.showinfo("计算题",'没有更多类型了,如需帮助,请按"类型"按钮')
def jst_exit():
win.quit()
win.destroy()
def more():
tk.messagebox.showinfo("类型","开发中...")
def types():
tk.messagebox.showinfo("类型","""1代表加法,2代表减法,3代表乘法,4代表除法
只需在题目类型输入框中填入对应的数字即可""")
labname=tk.Label(win,text='题目数量:',width=80)
labpwd=tk.Label(win,text='题目类型:',width=200)
entname=tk.Entry(win,width=100,textvariable=var_num)
entpwd=tk.Entry(win,width=100,textvariable=var_type)
but_Ok=tk.Button(win,text='开始',command=start)
but_Cancel=tk.Button(win,text='退出',command=jst_exit)
but_quit=tk.Button(win,text='可选',command=more)
but_type=tk.Button(win,text='类型',command=types)
labname.place(x=20,y=10,width=80,height=20)
labpwd.place(x=20,y=40,width=80,height=20)
entname.place(x=100,y=10,width=140,height=20)
entpwd.place(x=100,y=40,width=80,height=20)
but_Ok.place(x=30,y=80,width=50,height=20)
but_Cancel.place(x=110,y=80,width=50,height=20)
but_quit.place(x=190,y=80,width=50,height=20)
but_type.place(x=190,y=40,width=50,height=20)
win.mainloop()