In this article, we will go through each of the steps required to create a text editor using Python and guizero.
Share this step
Getting started
First, we need to plan our text editor by thinking about what it needs to do and which widgets are best suited to the task. Look at an existing text editor for inspiration (open WordPad, Notepad++ or similar, and click on the file icon (or equivalent) to see some options that the text editor gives you).
You’re going to build a very basic text editor in this step. For this you will need:
A TextBox to write into
A TextBox to specify a filename
A PushButton to save our file
A PushButton to open saved files
Take a moment to design the layout of your text editor, incorporating all four elements. Will you put the save button at the top or bottom of your GUI? Will it be positioned next to the open button? How big do you want your TextBox to be?
I’m going to work through adding each component. Try to modify the code as we go so that your text editor looks like your design. Remember that guizero adds widgets in the order they are created, starting from the top. This means you might have to reorder the following code to recreate your design.
I’ll begin by designing each widget that our text editor is comprised of, before writing functions to make them work as a text editor.
My text editor will have all of the controls at the top and a TextBox filling the rest of the screen. From left to right, there will be a box for a file name, a button to open files, and a button to save them.
The easiest way to arrange these controls and keep them together is to put them in a Box. It’s named file_controls because it houses the controls for creating and saving a file, but you could name yours something else as long as it clearly describes what the box contains.
fromguizeroimportApp,TextBox,PushButton,Boxapp=App(title="texteditor")# create a box to house the controls, we want the box to span the entire width of the app
file_controls=Box(app,align="top",width="fill")# create a TextBox for the file name
file_name=TextBox(file_controls,text="text_file.txt",width=50,align="left")# create a save button which uses the save_file function
save_button=PushButton(file_controls,text="Save",align="right")# create an open button which uses the open_file function
open_button=PushButton(file_controls,text="Open",align="right")# create a TextBox which is not in the box and fills the rest of the GUI
editor=TextBox(app,multiline=True,height="fill",width="fill")app.display()
Currently, the save and open buttons don’t do anything. In order to instruct them to open or save a file, we need to create two functions.
# function for reading files
defopen_file():withopen(file_name.value,"r")asf:editor.value=f.read()# function for writing files
defsave_file():withopen(file_name.value,"w")asf:f.write(editor.value)
These functions use the Python function open. If you are unfamiliar with it, you can read more here.
Before reading further, try to work out what the parameters file_name.value and editor.value are and determine what each function will do when called.
The open_file() function opens a file where the name of the file matches the value property of the TextBoxfile_name. It then sets the value property of the editorTextBox to be the contents of the file, i.e. it loads the text contents of the file into the editorTextBox.
The save_file() function performs the reverse. Can you deduce what file_name.value and editor.value are in this case?
Finally, I need to instruct the open and save PushButtons to call their respective functions when they are activated. This is done by setting the command parameter of each to the correct function, i.e. by replacing line 11 above with
Can you make the equivalent edit to the open_button widget?
When your text editor is functioning, you can work on making it more interesting by giving its users more style options. We’ll do this in the next step so remember to save your code.