Write a program that allows the user to practice typing. When the user clicks a button, the program generates and displays a random sequence of letters in an Entry component. The user repeats the sequence in another Entry component. When the user enters an incorrect letter, the pro- gram displays an error message until the user types the correct letter. Use keyboard events.

What will be an ideal response?


```
# Typing teacher program

from Tkinter import *
from tkMessageBox import *
import random

class typingTeacher( Frame ):
"""Typing Teacher class"""

alphabet = "abcdefghijklmnopqrstuvwxyz"

def __init__( self ):
"""Create two Entries and bind keyboard events"""

Frame.__init__( self )
self.grid( sticky = W+E+N+S )
self.master.title( "Typing Teacher" )
self.master.rowconfigure( 0, weight = 1 )
self.master.columnconfigure( 0, weight = 1 )

# Entry to hold practice word
self.practice = Entry( self )
self.practice.grid( row = 0, sticky = W+E, padx = 5,
pady = 5 )
self.practice.insert( INSERT, "PRACTICE WORD" )

# Entry to hold typing practice
self.entry = Entry( self )
self.entry.grid( row = 2, sticky = W+E, padx = 5,
pady = 5 )
self.entry.insert( INSERT, "Type here." )

# keyboard event bound to Entry
self.entry.bind( "", self.keyPressed )

# Button
self.startButton = Button( self, text = "Start",
command = self.start )
self.startButton.grid( row = 4, column = 0, padx = 5,
pady = 5 )

# Text area for error messages
self.errorBox = Text( self, width = 25, height = 5 )
self.errorBox.grid( row = 6, column = 0, sticky = W+E )
self.errorBox.insert( INSERT,
"Error messages displayed here." )

def generateWord( self ):
"""Generate random sequence of letters"""

self.practiceWord = ""

# creates a 20-letter word
for i in range( 0, 21 ):
self.practiceWord += \
self.alphabet[ random.randrange( 0, 26 ) ]
def start( self ):
"""Start typing teacher"""

# clear two Entry fields and Text area
self.entry.delete( 0, END )
self.practice.delete( 0, END )
self.index = 0 # starts at beginning

# generate and insert practice "word"
self.generateWord()
self.practice.insert( INSERT, self.practiceWord )

def keyPressed( self, event ):
"""Inform user if the incorrect key was pressed"""

self.errorBox.delete( 1.0, END )

# ignore backspace when user corrects error
if event.char in self.alphabet:

if event.char != self.practiceWord[ self.index ]:
self.entry.delete( self.index )
errorMessage = "Wrong key! You selected " + \
event.char + " rather than " + \
self.practiceWord[ self.index ] +\
". Try again."
self.errorBox.insert( INSERT, errorMessage )
else: self.index += 1

def main():
typingTeacher().mainloop()

if __name__ == "__main__":
main()
```
![15065|336x129](upload://uiFYElaAMfiEHG90uKTmaU8rzi9.png)

Computer Science & Information Technology

You might also like to view...

Given the following definition and initialization. Write a code fragment including a loop that will overwrite the string greeting, with ‘X’ in all character position, keeping the length the same.

What will be an ideal response?

Computer Science & Information Technology

Tiling is when a browser loads a background image, then repeats the image in both the vertical and horizontal directions until the entire background is filled.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

_______ is the name of the IBM computer designed for chess.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

What is File name extension?

What will be an ideal response?

Computer Science & Information Technology