Python Tkinter 口算练习程序
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import random
import math
import time
# 共使用四个标准库 tkinter, random, math, time 以及一个第三方库 matplotlib
global x, tot, ri
x = 0
tot = int(5)
ri = int(0)
class Countdown:
def __init__(self, master, seconds):
self.master = master
self.seconds = seconds
self.label = tk.Label(master, text='')
self.label.pack()
self.remaining = 0
self.countdown()
def countdown(self):
if self.seconds == -1:
self.label.configure(text='时间到了!')
global x
x = 1
else:
self.label.configure(text='倒计时: %d s' % self.seconds)
self.seconds -= 1
self.remaining = self.master.after(1000, self.countdown)
def chuan():
global root
root = Tk()
root.geometry('310x200+600+320')
root.title('加载中...')
lab=ttk.Label(root,text='正在创建题目,请稍等!').pack(pady=20)
txt1=StringVar()
txt1.set(' ')
lab=ttk.Label(root,textvariable=txt1).pack(pady=10)
progress=ttk.Progressbar(root)
progress.pack(side=LEFT,padx=10)
progress['length']=200
progress['maximum']=100
progress['value']=0
txt=StringVar()
txt.set('加载')
lab=ttk.Label(root,textvariable=txt).pack(side=LEFT,padx=5,pady=5)
a=0
for i in range(100):
progress['value'] = i + 1
root.update()
time.sleep(0.02)
a+=1
b=round(a,1)
progress['value']=b
st='创建'+str(b)+'% '
txt.set(st)
if progress['value']==100:
txt1.set('加载完成,点击确认开始答题')
messagebox.showinfo('完成', '题目创建完成,请在60秒内完成答题!')
else:
continue
root.destroy()
def chuaner():
def ys():
global tot, ri
ans1 = entry01.get()
ans2 = entry02.get()
ans3 = entry03.get()
ans4 = entry04.get()
ans5 = entry05.get()
f1 = f2 = f3 = f4 = f5 = True
if c1 == 0 and a1 + b1 == int(ans1):
ri += 1
elif c1 == 1 and a1 - b1 == int(ans1):
ri += 1
elif c1 == 2 and a1 * b1 == int(ans1):
ri += 1
else:
f1 = False
if c2 == 0 and a2 + b2 == int(ans2):
ri += 1
elif c2 == 1 and a2 - b2 == int(ans2):
ri += 1
elif c2 == 2 and a2 * b2 == int(ans2):
ri += 1
else:
f2 = False
if c3 == 0 and a3 + b3 == int(ans3):
ri += 1
elif c3 == 1 and a3 - b3 == int(ans3):
ri += 1
elif c3 == 2 and a3 * b3 == int(ans3):
ri += 1
else:
f3 = False
if c4 == 0 and a4 + b4 == int(ans4):
ri += 1
elif c4 == 1 and a4 - b4 == int(ans4):
ri += 1
elif c4 == 2 and a4 * b4 == int(ans4):
ri += 1
else:
f4 = False
if c5 == 0 and a5 + b5 == int(ans5):
ri += 1
elif c5 == 1 and a5 - b5 == int(ans5):
ri += 1
elif c5 == 2 and a5 * b5 == int(ans5):
ri += 1
else:
f5 = False
dic1 = {True: '正确', False: '错误'}
ret = Tk()
ret.title('答题正确率')
ret.geometry('500x700+300+200')
global x
if x == 1:
labtime = ttk.Label(ret, text='超时完成', font=20)
labtime.pack()
else:
labtime = ttk.Label(ret, text='恭喜你按时完成!', font=20)
labtime.pack()
labans = ttk.Label(ret, text=f'题目一{dic1.get(f1)}\n题目二{dic1.get(f2)}\n'
f'题目三{dic1.get(f3)}\n题目四{dic1.get(f4)}\n'
f'题目五{dic1.get(f5)}')
labans.pack()
tem = round(ri * 100 / tot)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots(figsize=(6, 6))
wedgeprops = {'edgecolor': 'black', 'linewidth': 3}
ax.pie([tem, 100 - tem], wedgeprops=wedgeprops, startangle=90, colors=['#5DADE2', '#515A5A'])
plt.title('答题正确率', fontsize=24, loc='center')
plt.text(0, 0, f'{tem}%', ha='center', va='center', fontsize=42)
# plt.show()
canvas = FigureCanvasTkAgg(fig, master=ret)
canvas.draw()
canvas.get_tk_widget().pack()
win = Tk()
win.title('答题界面')
win.geometry('500x500+500+100')
fu = ['+', '-', 'x']
count = Countdown(win, 60)
a1 = random.randrange(1, 101)
b1 = random.randrange(1, 101)
c1 = random.randrange(0, 3)
lab01 = ttk.Label(win, text=f'题目一: {a1} {fu[int(c1)]} {b1} = ?')
lab01.pack()
v1 = StringVar()
entry01 = ttk.Entry(win, textvariable=v1)
entry01.pack()
a2 = random.randrange(1, 101)
b2 = random.randrange(1, 101)
c2 = random.randrange(0, 3)
lab02 = ttk.Label(win, text=f'题目二: {a2} {fu[int(c2)]} {b2} = ?')
lab02.pack()
v2 = StringVar()
entry02 = ttk.Entry(win, textvariable=v2)
entry02.pack()
a3 = random.randrange(1, 101)
b3 = random.randrange(1, 101)
c3 = random.randrange(0, 3)
lab03 = ttk.Label(win, text=f'题目三: {a3} {fu[int(c3)]} {b3} = ?')
lab03.pack()
v3 = StringVar()
entry03 = ttk.Entry(win, textvariable=v3)
entry03.pack()
a4 = random.randrange(1, 101)
b4 = random.randrange(1, 101)
c4 = random.randrange(0, 3)
lab04 = ttk.Label(win, text=f'题目四: {a4} {fu[int(c4)]} {b4} = ?')
lab04.pack()
v4 = StringVar()
entry04 = ttk.Entry(win, textvariable=v4)
entry04.pack()
a5 = random.randrange(1, 101)
b5 = random.randrange(1, 101)
c5 = random.randrange(0, 3)
lab05 = ttk.Label(win, text=f'题目五: {a5} {fu[int(c5)]} {b5} = ?')
lab05.pack()
v5 = StringVar()
entry05 = ttk.Entry(win, textvariable=v5)
entry05.pack()
btn01 = ttk.Button(win, text='核对答案', command=ys)
btn01.pack()
btnquit = ttk.Button(win, text='退出', command=quit)
btnquit.pack()
win.mainloop()
rot=Tk()
rot.title('口算题卡启动程序')
rot.geometry('400x150+600+300')
lab=ttk.Label(rot,text='欢迎来到口算练习平台')
lab.pack(pady=10)
def fun():
time.sleep(1)
rot.destroy()
time.sleep(1)
chuan()
time.sleep(1)
chuaner()
bt=ttk.Button(rot,text='开始', command=fun)
bt.pack(pady=10)
bt=ttk.Button(rot,text='退出', command=quit)
bt.pack(pady=10)
rot.mainloop()
原文地址: https://www.cveoy.top/t/topic/g5Vx 著作权归作者所有。请勿转载和采集!