使用Python编写打字训练小程序升级版

升级版:主要功能是根据data.txt文件中的内容进行拼写训练

type.py

# -*- coding: utf-8 -*-
# @Software : PyCharm
# @version  :Python 3.7.3
# @File     : type.py

from tkinter import *
import random
import string
from datetime import datetime
 
root = Tk()
root.title("Python拼写练习")
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
v4 = StringVar()
e1 = Entry(root, text=v1, state='disabled', width=40, font=('宋体', 14))
e2 = Entry(root, textvariable=v2, width=40, font=('宋体', 14))
e3 = Label(root, textvariable=v3, width=40, font=('宋体', 10), foreground='red')
e4 = Label(root, textvariable=v4, width=15, font=('宋体', 10))
e1.grid(row=0, column=0, padx=12, pady=20)
e2.grid(row=1, column=0, padx=12, pady=20)
e3.grid(row=2, column=0, padx=12, pady=20)
e4.grid(row=3, column=0, padx=12, pady=5, sticky=E)
text = Text(root, width=57, height=7)
text.grid(row=4, column=0, sticky=W, columnspan=2, padx=12, pady=20)
 
 
class TypingTest:
    def __init__(self):
        file=open(r'data.txt','r', encoding='UTF-8')
        alllines = file.readlines()
        file.close()
        self.answerList=[]  
        self.requesetList=[]
        for line in alllines:
            curLine=line.strip().split(",")
            self.answerList.append(curLine[0])
            self.requesetList.append(curLine[1])
        self.create_exam()
        v4.set("按回车提交!")
 
    def create_exam(self):
        self.requeset = self.requesetList[0]
        self.answer = self.answerList[0]
        self.examination_paper = ''
        text.delete(0.0, END)
        v1.set(self.requeset)
        v2.set("")
 
    def score(self, event):
        text.delete(0.0, END)
        wrong_index = []
        rightString = ''
        self.examination_paper = v2.get()
        for i in range(len(self.answer) if (len(self.answer) > len(self.examination_paper)) else len(self.examination_paper)):
            if i >= (len(self.examination_paper) if (len(self.answer) > len(self.examination_paper)) else len(self.answer)):
                wrong_index.append(i)
                rightString += " "
            elif self.examination_paper[i] != self.answer[i]:
                wrong_index.append(i)
        if self.examination_paper == self.answer:
            v3.set("回答正确,继续下一题。")
            del self.requesetList[0]
            del self.answerList[0]
            e3['foreground']='green'
            self.create_exam()
        else:
            v3.set("回答错误!")
            e3['foreground']='red'
            text.insert(END, "题目:%s\n" % self.requeset)
            tag_info = list(map(lambda x: '2.' + str(x + 3), wrong_index))
            text.insert(END, "答案:%s%s\n" % (self.examination_paper,rightString))
            for i in range(len(tag_info)):
                text.tag_add("tag1", tag_info[i])
                text.tag_config("tag1", background='red')
            text.tag_add("tag2", '2.'+str(wrong_index[-1] + 4))
            text.tag_config("tag2", background='white')
TypingTest = TypingTest()
e2.bind(sequence="", func=TypingTest.score)
root.geometry("430x350")
mainloop()

data.txt

test,测试1
test2,测试2

使用Python编写打字训练小程序升级版_第1张图片

你可能感兴趣的:(A-Python)