1、适应小孩子爱捣蛋的特点,所有输入正确答案之外的输入,界面上不会再有变化。
2、输入正确答案后会随机出现奥特曼图片,吸引小孩子继续“玩”。
3、加入随机功能后,原来正常显示的图片,会出现白屏的情况,加入一行
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() a = window.winfo_screenwidth() #1920 b = window.winfo_screenheight() #1080 print(a, b) window.geometry("%dx%d"%(a,b)) pt2= tk.PhotoImage(file = path1 +"/奥特曼图片/小21.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) fm2 = tk.Frame(window) fm2.pack(anchor="nw", side="top", fill="both", padx=5, pady=5) #l = tk.Label(window, text=calc, fg="red", bg="white", font = "黑体 100", image=pt2, compound="center") l = tk.Label(fm2, text=calc, fg="red", font = "黑体 200") l.grid(row=0, column=0, padx=5, pady=5) ll = tk.Label(fm2, fg="red", image=pt2, compound="center") ll.grid(row=0, column=1, padx=5, pady=5, sticky="e") fm2 = tk.Frame(window) fm2.pack(anchor="nw", side="top", fill="both", padx=5, pady=5) pic1 = "☯☯☯☯☯☯☯☯☯☯" pic2 = "✸✸✸✸✸✸✸✸✸✸" pic3 = pic1[0:x] pic4 = pic2[0:y] pic_lb1 = tk.Label(fm2, text=pic3, font = "宋体 30", bg="light blue", wraplength=200, height=2, width=int(22*a/1440)) pic_lb1.grid(row=0, column=0, padx=5, pady=5) pic_lb2 = tk.Label(fm2, text=pic4, font = "宋体 30", bg="pink",wraplength=200, height=2, width=int(22*a/1440)) pic_lb2.grid(row=0, column=1, padx=5, pady=5) fm1 = tk.Frame(window) fm1.pack(anchor="nw", side="top", fill="both", padx=5, pady=5) lb1 = tk.Label(fm1, text="正确率:",font="宋体 14") lb1.grid(row=0, column=0, padx=5, pady=5) pb1 = ttk.Progressbar(fm1) #正确率 pb1.grid(row=0, column=1, ipadx=a/10, padx=5, pady=5) lb2 = tk.Label(fm1, text="",font="宋体 14") lb2.grid(row=0, column=2, padx=5, pady=5) lb3 = tk.Label(fm1, text="平均用时:",font="宋体 14") lb3.grid(row=1, column=0, padx=5, pady=5) pb2 = ttk.Progressbar(fm1) #平均用时 pb2.grid(row=1, column=1, ipadx=a/10, padx=5, pady=5) lb4 = tk.Label(fm1, text="",font="宋体 14") lb4.grid(row=1, column=2, padx=5, pady=5) #pb3 = ttk.Progressbar(window) l2 = tk.Label(window, bg="light blue", fg = "red", font = "宋体 14", height=1) l2.pack(anchor="nw", side="top",fill="x", padx=5, pady=5) e = tk.Entry(window, font="黑体 20", fg="red", bg="yellow") e.pack(anchor="center", side="top", padx=5, pady=5, ipadx=10, ipady=10) pb1["maximum"] = 1 pb1["value"] = 0 pb2["maximum"] = 30 def submit_result(): nonlocal x nonlocal y nonlocal calc nonlocal start_time nonlocal end_time nonlocal init_time nonlocal m nonlocal n nonlocal path inp = e.get() print(e.get()) try: if int(inp) == x+y: n = n+1 end_time = dt.datetime.now() time_used = end_time - start_time avg_time = (end_time - init_time)/n m = m+1 l2.config(text = "正确!!", fg="red") path_ultraman= path1 +"/奥特曼图片" pt_ultraman_list = os.listdir(path_ultraman) print(len(pt_ultraman_list)) i = random.randint(0,len(pt_ultraman_list)) print("i:", i) print(pt_ultraman_list[i]) print(path_ultraman) pt1= tk.PhotoImage(file = path_ultraman +"/"+ pt_ultraman_list[i]) print(pt1) ll.config(image = pt1, compound="center") ll.image = pt1 #没有这一句,就白屏。 pb1["value"] = m/n pb2["value"] = 30 - avg_time.seconds lb2.config(text="正确数%d,总题数%d。" %(m,n)) lb4.config(text="%d秒,本次用时%s秒。"%(avg_time.seconds,str(time_used.seconds))) print("正确!!") 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) pic3 = pic1[0:x] pic4 = pic2[0:y] pic_lb1.config(text=pic3) pic_lb2.config(text=pic4) print(x, y) else: n = n+1 e.delete(0, tk.END) pass except: e.delete(0, tk.END) pass 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()