0
点赞
收藏
分享

微信扫一扫

给儿子做的加法算术小程序

四月天2021 2022-03-12 阅读 98

 

代码:

import tkinter as tk
from tkinter import ttk
import random
import time
import datetime as dt
import os



def main():
    path = os.getcwd()
    print(path)
    path1 = path.replace("\\", "/")
    print(path1)
    
    window = tk.Tk()
    window.geometry("800x800")

    pt1= tk.PhotoImage(file = path1 + "/lvwugui.png")
    pt2= tk.PhotoImage(file = path1 + "/baiwugui.png")

    init_time = dt.datetime.now()
    start_time = dt.datetime.now()
    end_time = dt.datetime.now()
    
    m=0 #正确次数
    n=0 #总次数
    
        
    x = random.randint(1, 10)
    y = random.randint(1, 10)
    calc = "%d + %d" %(x, y)
    l = tk.Label(window, text=calc, fg="red", bg="white", font = "黑体 100", image=pt2, compound="center")
    l.pack(anchor="nw", side="top", fill="both", padx=5, pady=5)
    
    l2 = tk.Label(window, bg="steel blue", font = "宋体 14", height=3)
    l2.pack(anchor="nw", side="top",fill="x", padx=5, pady=5)
    
    #l3 = tk.Label(window,image=pt2)
    #l3.pack(anchor="center", fill="both", ipadx=40, ipady=40)
    
    e = tk.Entry(window, font="黑体 20", fg="red", bg="yellow")
    e.pack(anchor="center", side="top", padx=5, pady=5, ipadx=10, ipady=10)

    def submit_result():
        nonlocal x
        nonlocal y
        nonlocal calc
        nonlocal start_time
        nonlocal end_time
        nonlocal init_time
        nonlocal m
        nonlocal n
        nonlocal path
        nonlocal pt1
        nonlocal pt2

        n = n+1
        #nonlocal time_used
        #print(x, y, calc)
        inp = e.get()
        #print(inp, x+y)
        end_time = dt.datetime.now()
        time_used = end_time - start_time
        #time_used_str = time_used.strftime("%H:%M:%S")
        avg_time = (end_time - init_time)/n
        print(start_time, end_time, time_used)
        if inp!="":
            
            if int(inp) == x+y:
                m = m+1            
                l2.config(text = "正确!! \n正确数%d,总题数%d, 正确率是%f。\n用时%s,平均用时%s" \
                          %(m, n, m/n, str(time_used), str(avg_time)), fg="red", bg="green")
                l.config(image = pt1)
                
                print("正确!!")
            
            else:                
                l2.config(text = "错误!! 正确答案是:%d。\n正确数%d,总题数%d, 正确率是%f。\n用时%s,平均用时%s"\
                          %(x+y, m, n, (m/n), str(time_used), str(avg_time)) , fg="blue", bg="pink")
                l.config(image = pt2)
                
                print("错误!!")
        else:
            l2.config(text = "未输入有效答案!! ", fg="yellow", bg="steel blue")            
            
            
        
        e.delete(0, tk.END)
        #time.sleep(3)
        #l2.config(text = "")
        start_time = dt.datetime.now()
        x = random.randint(1, 10)
        y = random.randint(1, 10)
        calc = "%d + %d" %(x, y)
        l.config(text=calc, bg="white")
        
        print(x, y)

    def submit_result_enter(ev=None):
        submit_result()

    e.bind("<Return>", submit_result_enter)        
            

    b = ttk.Button(window, text="提交", command = submit_result)
    b.pack(anchor="center", side="top", padx=5, pady=5)

    window.mainloop()

main()
举报

相关推荐

0 条评论