当前位置: 首页 » 综合知识 » it知识 » 正文

Python打字训练的程序怎么实现

发布时间:2023-07-23 以下文章来源于网友投稿,内容仅供参考!

1、键盘上的字符需要生成,string模块生成字符。

当然可以0-9,A-Z,a-z!等等,把键盘上的按键一个个举出来。

import string
# 列举数字
string.digits
>>> '0123456789'
# 列举小写字母
string.ascii_lowercase
>>> 'abcdefghijklmnopqrstuvwxyz'
# 列举大写字母
string.ascii_uppercase
>>> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# 列举所有标点符号
string.punctuation
>>> '!"#$%&\'()*+,-./:;?@[\\]^_`{|}~'
# 列举所有空白符
string.whitespace
>>> ' \t\n\r\x0b\x0c'
 
string.ascii_letters =
    string.ascii_lowercase + string.ascii_uppercase
string.printable =
    string.ascii_letters + string.digits
    + string.whitespace + string.punctuation

2、判断剩余内容的相关读写。

from tkinter import *
import random
import string
from datetime import datetime
 
root = Tk()
root.title("Python打字练习题 By:清风Python")
Label(root, text='系统题目:').grid(row=0)
Label(root, text='用户作答:').grid(row=1)
Label(root, text='考试结果:').grid(row=2)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
v1.set("点击'开始测试'按钮开始出题")
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')
e1.grid(row=0, column=1, padx=10, pady=20)
e2.grid(row=1, column=1, padx=10, pady=20)
e3.grid(row=2, column=1, padx=10, pady=20)
text = Text(root, width=80, height=7)
text.grid(row=4, column=0, columnspan=2, pady=5)
 
 
class TypingTest:
    def __init__(self):
        self.time_list = []
        self.letterNum = 20
        self.letterStr = ''.join(random.sample(string.printable.split(' ')[0], self.letterNum))
        self.examination_paper = ''
 
    def time_calc(self):
        self.time_list.append(datetime.now())
        yield
 
    def create_exam(self):
        text.delete(0.0, END)
        # e3.delete(0, END)
        v1.set(self.letterStr)
        self.time_calc().__next__()
        text.insert(END, "开始:%s \n" % str(self.time_list[-1]))
        user_only1.config(state='active')
 
    def score(self):
        wrong_index = []
        self.time_calc().__next__()
        text.insert(END, "结束:%s\n" % str(self.time_list[-1]))
        use_time = (self.time_list[-1] - self.time_list[-2]).seconds
        self.examination_paper = v2.get()
        if len(self.examination_paper) > self.letterNum:
            v3.set("输入数据有误,作答数大于考题数")
        else:
            right_num = 0
            for z in range(len(self.examination_paper)):
                if self.examination_paper[z] == self.letterStr[z]:
                    right_num += 1
                else:
                    wrong_index.append(z)
            if right_num == self.letterNum:
                v3.set("完全正确,正确率%.2f%%用时:%s秒" % ((right_num * 1.0) / self.letterNum * 100, use_time))
            else:
                v3.set("正确率%.2f%%用时:%s 秒" % ((right_num * 1.0) / self.letterNum * 100, use_time))
                # e2.delete(0, END)
                text.insert(END, "题目:%s\n" % self.letterStr)
                tag_info = list(map(lambda x: '4.' + str(x + 3), wrong_index))
                text.insert(END, "作答:%s\n" % self.examination_paper)
                for i in range(len(tag_info)):
                    text.tag_add("tag1", tag_info[i])
                    text.tag_config("tag1", background='red')
                    user_only1.config(state='disabled')
 
 
TypingTest = TypingTest()
Button(root, text="开始测试", width=10, mand=TypingTest.create_exam).grid(row=3, column=0, sticky=W, padx=30, pady=5)
user_only1 = Button(root, text="交卷", width=10, mand=TypingTest.score, state='disable')
user_only1.grid(row=3, column=1, sticky=E, padx=30, pady=5)
 
mainloop()

3、将最终代码打包成exe工具,可以脱离python环境,在单独的电脑上执行exe文件,就可以打字练习了。

  • • Linux Ecdsa密钥长度选择有何依据

    在Linux

  • • Linux Khook在内核监控中的应用如何

    Linux

  • • Linux Gsoap是否支持异步通信

    GSOAP是

  • • Linux Coremail如何提升用户体验

    提升Linu

  • • Linux Ecdsa算法有哪些局限性

    ECDSA

  • 哎呀音乐钢琴键盘学习《 钢琴主人训练营》 西瓜学琴
    郭蝈 陪练钢琴 30节课时 考级刚需 让孩子每一次练琴都是高质量的
    30天轻松学会五线谱 流行钢琴自学初级教程 牙牙学琴
    流行爵士钢琴实战技巧VIP课 - 继伟 哎呀音乐
    【海上钢琴师】原版 MT1990钢琴谱
    百首经典流行钢琴实战曲集 - 继伟
    雷费尔德电钢琴重锤88键专业考级儿童初学者数码电子钢琴家用
    小练咖 真人钢琴陪练 1v1服务 2999随时退 1课时50分钟 考级刚需
    雅马哈电钢琴88键重锤CLP735智能数码电子钢琴家用专业初学者考级
    【原装进口】Yamaha/雅马哈钢琴 b121 SC2原声静音钢琴
  • 珠海专业调钢琴
  • 天津宝坻区调钢琴
  • 天津静海区钢琴调音
  • 成都简阳市钢琴调律
  • 大连瓦房店市钢琴调音
  • 眉山调钢琴联系方式
  • 惠州大亚湾钢琴调琴师
  • 长治调琴师
  • 厦门湖里区钢琴调音师
  • 上海普陀区钢琴调音师