• 技术文章 >Python技术 >Python基础教程

    Python如何实现打字训练的程序

    小妮浅浅小妮浅浅2021-09-11 09:50:27原创3963

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

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    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、判断剩余内容的相关读写。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    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, command=TypingTest.create_exam).grid(row=3, column=0, sticky=W, padx=30, pady=5)

    user_only1 = Button(root, text="交卷", width=10, command=TypingTest.score, state='disable')

    user_only1.grid(row=3, column=1, sticky=E, padx=30, pady=5)

      

    mainloop()

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

    以上就是Python实现打字训练程序的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python程序
    上一篇:Python lambda的速写用法 下一篇:Python列表推导式如何使用

    相关文章推荐

    • pycharm如何运行python程序• 如何使用pycharm跑Python程序• 批处理怎么执行Python程序• 别人怎么用我的Python程序• Python程序怎么变成模块• 怎么计算python程序运行时间• 几个简单的python程序分享• python程序中怎么将文件存储至目录• python程序出错删除一行代码怎么做• 怎么在命令提示符中运行python程序• 怎样在linux上执行python程序• 如何在dos命令窗口运行python程序• CMD无法运行python程序怎么办• python程序如何实现接口封装、请求、调用?• python程序的执行原理

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网