新聞中心
1、鍵盤(pán)上的字符需要生成,string模塊生成字符。

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)成都VR全景等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站營(yíng)銷優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
當(dāng)然可以0-9,A-Z,a-z!等等,把鍵盤(pán)上的按鍵一個(gè)個(gè)舉出來(lái)。
import string
# 列舉數(shù)字
string.digits
>>> '0123456789'
# 列舉小寫(xiě)字母
string.ascii_lowercase
>>> 'abcdefghijklmnopqrstuvwxyz'
# 列舉大寫(xiě)字母
string.ascii_uppercase
>>> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# 列舉所有標(biāo)點(diǎn)符號(hào)
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.punctuation2、判斷剩余內(nèi)容的相關(guān)讀寫(xiě)。
from tkinter import *
import random
import string
from datetime import datetime
root = Tk()
root.title("Python打字練習(xí)題 By:清風(fēng)Python")
Label(root, text='系統(tǒng)題目:').grid(row=0)
Label(root, text='用戶作答:').grid(row=1)
Label(root, text='考試結(jié)果:').grid(row=2)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
v1.set("點(diǎn)擊'開(kāi)始測(cè)試'按鈕開(kāi)始出題")
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, "開(kāi)始:%s \n" % str(self.time_list[-1]))
user_only1.config(state='active')
def score(self):
wrong_index = []
self.time_calc().__next__()
text.insert(END, "結(jié)束:%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("輸入數(shù)據(jù)有誤,作答數(shù)大于考題數(shù)")
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%%用時(shí):%s秒" % ((right_num * 1.0) / self.letterNum * 100, use_time))
else:
v3.set("正確率%.2f%%用時(shí):%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="開(kāi)始測(cè)試", 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環(huán)境,在單獨(dú)的電腦上執(zhí)行exe文件,就可以打字練習(xí)了。
以上就是Python實(shí)現(xiàn)打字訓(xùn)練程序的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)頁(yè)標(biāo)題:創(chuàng)新互聯(lián)Python教程:Python如何實(shí)現(xiàn)打字訓(xùn)練的程序
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/dpogcjp.html


咨詢
建站咨詢
