Create a simple text-editor GUI that allows the user to open a file. The GUI should display the text of the file and then close the file. The user can modify the file’s contents. When the user chooses to save the text, the modified contents should be written to the file, replacing any other con- tents. The user also should be able to clear the display.
What will be an ideal response?
```
# Simple text editor.
from Tkinter import *
import Pmw
class TextEditor( Frame ):
"""Allow user to open, edit, save and close a file"""
def __init__( self ):
"""Create MenuBar and ScrolledText components"""
Frame.__init__( self )
Pmw.initialise()
self.grid( sticky = N+E+W+S )
self.master.title( "Text Editor" )
self.choices = Pmw.MenuBar( self )
self.choices.grid( row = 0, sticky = E+W )
self.choices.addmenu( "File", None )
self.choices.addmenuitem( "File", "command",
label = "Open", command = self.displayOpenEntry )
self.choices.addmenuitem( "File", "command",
label = "Save", command = self.saveFile )
self.choices.addmenuitem( "File", "command",
label = "Clear", command = self.clearDisplay )
self.textBox = Pmw.ScrolledText( self )
self.textBox.grid( rowspan = 10, columnspan = 3 )
def displayOpenEntry( self ):
"""Display Entry for file name"""
self.openFileEntry = Entry( self )
self.openFileEntry.grid( row = 11, column = 0,
columnspan = 2, sticky = E+W )
self.openFileButton = Button( self, text = "Open",
command = self.openFile )
self.openFileButton.grid( row = 11, column = 2,
sticky = E+W )
self.openFileEntry.insert( INSERT,
"Enter file name and press button." )
def openFile( self ):
"""Open file, display its contents and close it"""
self.fileName = self.openFileEntry.get()
self.openFileEntry.grid_forget()
self.openFileButton.grid_forget()
# open file for input and output
try:
self.file = open( self.fileName, "r+" )
# w+ creates file if the file does not exist
except:
self.file = open( self.fileName, "w+" )
# get text from file
text = self.file.readlines()
display = ""
for line in text:
display += line
# insert text in ScrolledText component
self.textBox.settext( display )
self.file.close()
def saveFile( self ):
"""Save file"""
self.file = open( self.fileName, "w+" )
print >> self.file, self.textBox.get()
self.file.close()
def clearDisplay( self ):
"""Clear display"""
self.textBox.clear()
def main():
TextEditor().mainloop()
if __name__ == "__main__":
main()
```

You might also like to view...
Which of the following statements about named constants is/are true?
A) A named constant must be initialized with a value at the time it is declared. B) The identifier name of a named constant can only contain capital letters and underscores. C) The initial value of a named constant, cannot be changed during the program execution. D) All 3 of the above statements are true. E) Statements A and C are true, but B is not.
A relationship between classes where one class automatically obtains the functions of another class is called:
A. composition B. inheritance C. polymorphism D. virtual
The number of frames shown in each unit of time is called the ____________________ and is commonly expressed as frames per second.
Fill in the blank(s) with the appropriate word(s).
A common source of user misunderstanding and subsequent calls to help desks is poorly designed ____.
A. hardware B. software C. user documentation D. networks