We can now continue with the Entry Object for Python.
Hopefully, you have gone over Part 1 before continuing on here.
Font
Since we can change the colors, why not the font as well? You need to make sure that any font you use exists on the system where the Python code is being executed.
On my system, I am running Ubuntu, so I have a font named ‘ubuntu’.
In the example, the label will have the text ‘Example Entry’ with the font being ‘ubuntu’:
You can also specify a size for the font with the parameter ‘font=(‘font_name’,#)’ where ‘#’ is the size. An example is:
The font and size can be changed for every label or object.
You can also add the following a comma and enclosed in quotes:
The text in the Entry box will be bold as well as underlined.
TextVariable
You can set up a variable to hold text that will be inserted into the Entry box. This is done in the same manner as a label.
Use the code:
Here, we create the variable named 'Var1' and designate it as a 'StringVar'. We create the Entry box and specify the use of the 'textvariable' and its variable name. We can then at any time in the code set the value for the variable to set it.
ExportSelection
When the focus on an Entry box is lost, the selected text will not be highlighted anymore. To prevent this, you can set the 'exportselection' option equal to 'false' to keep the highlight.
By default, the highlight is lost because the 'exportselection' option is set to 'true'.
For example, we can set an Entry to keep selection highlighting:
Focus
When moving from object to object using the TAB key, each object, if allowed, will have the current focus. The object with the current focus is active and can be used or changed. For example, a button with focus can allow the user to press ENTER so it acts if it were clicked.
In an Entry box, you can type characters or edit the existing characters, if allowed.
When an object has focus, there is usually a special border color to show that the object is in focus.
To prevent an object to have the ability to take focus, use the 'takefocus=False' option. By default, the Entry Box has a value of 'True'.
If the tkinter window has the focus over other windows, you can set the focus on a specific object by using the 'focus' or 'focus_set' option. These are the same options:
You can also force an application and a specific object to have the focus by:
DisabledBackground and DisabledForeground
If an Entry box is disabled, then you can set the color of the background and text.
By default, it is grayed out, but you can choose any color you want.
HighlightBackground and Highlightthickness
The color set by the 'highlightbackground' is the border color that is used when an Entry box has lost focus.
Keep in mind that the 'highlight' color is for the boxes that do NOT have focus.
The 'highlightthickness' is the number of pixels used for the width of the border whether or not the Entry has focus. The color is for the unfocused Entry boxes while the focused Entry box is black. It is best not to make the 'highlightbackground' set to black or all the boxes will look the same.
Border (bd)
This is the thickness of the border set around the Entry box. The default is 2 pixels.
The border is just inside the highlighted area, but outside the text background.
In Figure 1, the text background is blue, the highlight is red and the border is a light blue based on the background color:
FIGURE 1
The light blue border color is determined by the style of border used by the 'relief' parameter.
InsertBackground, InsertWidth, InsertBorderWidth, InsertOntime and InsertOfftime
When you have an Entry box and it gets focus, there is an insertion cursor that appears in it. The insertion cursor is by default set to black.
We can change the color of the insertion cursor with the option 'insertbackground'. The default is a width of '1', but we can make the width of the insertion cursor wider, in pixels, with the option 'insertwidth'.
Let's look at example code that will make the insertion cursor 'blue' and have a width of five pixels:
The 'insertborderwidth' is the border around the cursor in pixels.
The last two options, 'insetontime' and 'insertofftime', go together, but are not both required. You can specify how long the cursor stays on or off as it blinks. The values are in milliseconds, so 1000 milliseconds is a second. The two numbers can be different, so the cursor is on or off longer. Let's look at an example where the cursor is on for 3 seconds, then off for 1 second before coming on again:
Relief
The 'relief' is the look of the border around the Entry box. There are five different Entry box reliefs:
Just set the 'relief=' to one choice when creating an Entry box. For example, we can create an Entry:
The different 'reliefs' are shown in Figure 2.
FIGURE 2
STATE
The 'state' of the Entry box is a way to change the state for specific purposes.
There are three states:
If the state is set to 'disabled', then the Entry box cannot have the text edited, or even selected. The box will be grayed out.
For 'readonly', the text is for reading and cannot be changed. The box will look like a 'normal' state.
These can be changed if certain options are chosen. On one line, you can 'disable' the box and on another, after checking certain criteria, you can change to another state.
Justify
You can set the text to be justified in the Entry box. There are three options:
By default, the Entry box justification is set to 'left'.
You can change the justification of each box differently, if desired. An example of 'center' justification is:
ReadOnlyBackground
If you set the Entry box text to be read-only, you can set the background color to something different to stand out so the user knows it cannot be changed.
The following code shows an example:
The way this has worked for me is to initialize the Entry box, insert the text and then set it to 'readonly'. Once it is set to 'readonly', you cannot change the text in the program.
The last two lines let you set settings without the '.configure' statement.
Show
When a user types in sensitive information, such as a password, you can mask the characters being shown on the screen. You can use any single character you wish.
Once you set up the Entry box, you can specify what character to show:
If you insert text into the Entry box, it will be masked.
XscrollCommand
This is a command that connects the Entry box to a scroll bar. Granted, the Scrollbar is another object not covered yet, but I'll get to this lightly. The following code sets up a scrollbar for the Entry box if the text goes past the visible portion of the Entry box. The code is:
We set up an Entry box named 'e1'. We also set up a scrollbar named 'h_scroll' which has a command for 'e1.xview'. The Entry box only scrolls on the x-axis. The Entry box has an 'xscrollcommand' set to the scrollbar's horizontal scroll, 'h_scroll'. So the two are now linked for scrolling horizontally. More details will be given in the article for the 'Scrollbar'.
Cursor
If you move the cursor over an Entry box, you can change the cursor that is seen by a user. You can look at the article for Labels (Python 17 - Labels) to see the list. Use one of these cursor types is used for a button, the cursor reverts when it is no longer over the button.
An example is to make the cursor a 'boat' when it moves over the button:
Validate, ValidateCommand and InvalidCommand
When data is entered in an Entry box, we can set up a 'validate' event to occur when the event happens. There are six validation events:
A quick example is as follows:
So, we create an Entry, 'e1', and a label, 'l1'. The Entry gets focus when the code is executed. When setting up the Entry box, we set the 'validatecommand' to 'vcmd' when there is a 'key' event. In turn, 'vcmd' is set to running 'root.register' with the function 'validate_entry' and a parameter of '%P'. These parameters are as follows:
If you wanted to check for 'invalidcommand', the code would be:
These work the same way as the 'validatecommand', but just occur when it is invalid.
Change the event parameter as you need for the function to work correctly. Notice too that the key pressed is only placed into the Entry box when the 'validationcommand' returns TRUE.
Conclusion
There have been a lot of details covered in both of these articles. There are a lot of options that can be used for the Entry box.
If you plan on learning Python, even just to make minor programs, the Entry box can be a major widget used regularly. Look over these two carefully.
Hopefully, you have gone over Part 1 before continuing on here.
Font
Since we can change the colors, why not the font as well? You need to make sure that any font you use exists on the system where the Python code is being executed.
On my system, I am running Ubuntu, so I have a font named ‘ubuntu’.
In the example, the label will have the text ‘Example Entry’ with the font being ‘ubuntu’:
Code:
e1 = Entry(font='ubuntu')
e1.set
You can also specify a size for the font with the parameter ‘font=(‘font_name’,#)’ where ‘#’ is the size. An example is:
Code:
e1 = Entry(font=('ubuntu',18)
The font and size can be changed for every label or object.
You can also add the following a comma and enclosed in quotes:
- underline
- bold
- italic
Code:
e1 = Entry(font=('ubuntu',18,"bold","underline")
The text in the Entry box will be bold as well as underlined.
TextVariable
You can set up a variable to hold text that will be inserted into the Entry box. This is done in the same manner as a label.
Use the code:
Code:
Var1 = StringVar()
e1 = Entry(root, textvariable=Var1)
Var1.set("abcdefghij")
Here, we create the variable named 'Var1' and designate it as a 'StringVar'. We create the Entry box and specify the use of the 'textvariable' and its variable name. We can then at any time in the code set the value for the variable to set it.
ExportSelection
When the focus on an Entry box is lost, the selected text will not be highlighted anymore. To prevent this, you can set the 'exportselection' option equal to 'false' to keep the highlight.
By default, the highlight is lost because the 'exportselection' option is set to 'true'.
For example, we can set an Entry to keep selection highlighting:
Code:
e1=Entry(exportselection=FALSE)
Focus
When moving from object to object using the TAB key, each object, if allowed, will have the current focus. The object with the current focus is active and can be used or changed. For example, a button with focus can allow the user to press ENTER so it acts if it were clicked.
In an Entry box, you can type characters or edit the existing characters, if allowed.
When an object has focus, there is usually a special border color to show that the object is in focus.
To prevent an object to have the ability to take focus, use the 'takefocus=False' option. By default, the Entry Box has a value of 'True'.
If the tkinter window has the focus over other windows, you can set the focus on a specific object by using the 'focus' or 'focus_set' option. These are the same options:
Code:
e1.focus()
e1.focus_set()
You can also force an application and a specific object to have the focus by:
Code:
e1.focus_force()
DisabledBackground and DisabledForeground
If an Entry box is disabled, then you can set the color of the background and text.
By default, it is grayed out, but you can choose any color you want.
HighlightBackground and Highlightthickness
The color set by the 'highlightbackground' is the border color that is used when an Entry box has lost focus.
Keep in mind that the 'highlight' color is for the boxes that do NOT have focus.
The 'highlightthickness' is the number of pixels used for the width of the border whether or not the Entry has focus. The color is for the unfocused Entry boxes while the focused Entry box is black. It is best not to make the 'highlightbackground' set to black or all the boxes will look the same.
Border (bd)
This is the thickness of the border set around the Entry box. The default is 2 pixels.
The border is just inside the highlighted area, but outside the text background.
In Figure 1, the text background is blue, the highlight is red and the border is a light blue based on the background color:
FIGURE 1
The light blue border color is determined by the style of border used by the 'relief' parameter.
InsertBackground, InsertWidth, InsertBorderWidth, InsertOntime and InsertOfftime
When you have an Entry box and it gets focus, there is an insertion cursor that appears in it. The insertion cursor is by default set to black.
We can change the color of the insertion cursor with the option 'insertbackground'. The default is a width of '1', but we can make the width of the insertion cursor wider, in pixels, with the option 'insertwidth'.
Let's look at example code that will make the insertion cursor 'blue' and have a width of five pixels:
Code:
e1 = Entry(root, insertbackground="blue",insertwidth=5)
The 'insertborderwidth' is the border around the cursor in pixels.
The last two options, 'insetontime' and 'insertofftime', go together, but are not both required. You can specify how long the cursor stays on or off as it blinks. The values are in milliseconds, so 1000 milliseconds is a second. The two numbers can be different, so the cursor is on or off longer. Let's look at an example where the cursor is on for 3 seconds, then off for 1 second before coming on again:
Code:
e1 = Entry(root,insertontime=3000,insertofftime=1000)
Relief
The 'relief' is the look of the border around the Entry box. There are five different Entry box reliefs:
- FLAT (default)
- SUNKEN
- RAISED
- GROOVE
- SOLID
Just set the 'relief=' to one choice when creating an Entry box. For example, we can create an Entry:
Code:
e1=Entry(relief=RAISED)
The different 'reliefs' are shown in Figure 2.
FIGURE 2
STATE
The 'state' of the Entry box is a way to change the state for specific purposes.
There are three states:
- Normal
- Disabled
- Readonly
If the state is set to 'disabled', then the Entry box cannot have the text edited, or even selected. The box will be grayed out.
For 'readonly', the text is for reading and cannot be changed. The box will look like a 'normal' state.
These can be changed if certain options are chosen. On one line, you can 'disable' the box and on another, after checking certain criteria, you can change to another state.
Justify
You can set the text to be justified in the Entry box. There are three options:
- Left
- Center
- Right
By default, the Entry box justification is set to 'left'.
You can change the justification of each box differently, if desired. An example of 'center' justification is:
Code:
e1=Entry(justify='center')
ReadOnlyBackground
If you set the Entry box text to be read-only, you can set the background color to something different to stand out so the user knows it cannot be changed.
The following code shows an example:
Code:
e1 = Entry(root)
e1.insert(0,'Cannot Change')
e1['state'] = 'readonly'
e1['readonlybackground'] = 'pink'
The way this has worked for me is to initialize the Entry box, insert the text and then set it to 'readonly'. Once it is set to 'readonly', you cannot change the text in the program.
The last two lines let you set settings without the '.configure' statement.
Show
When a user types in sensitive information, such as a password, you can mask the characters being shown on the screen. You can use any single character you wish.
Once you set up the Entry box, you can specify what character to show:
Code:
e1.config(show='*')
If you insert text into the Entry box, it will be masked.
XscrollCommand
This is a command that connects the Entry box to a scroll bar. Granted, the Scrollbar is another object not covered yet, but I'll get to this lightly. The following code sets up a scrollbar for the Entry box if the text goes past the visible portion of the Entry box. The code is:
Code:
from tkinter import *
root = Tk()
root.title("Scrollable Entry Box")
root.geometry("400x800")
for row in range(0,9):
root.grid_rowconfigure(row, weight=1)
for col in range(0,3):
root.grid_columnconfigure(col, weight=1)
e1 = Entry(root, width=40)
e1.grid(column=0,row=0,sticky="S")
h_scroll = Scrollbar(root, orient='horizontal',width=10, command=e1.xview)
h_scroll.grid(column=0,row=1, sticky="NEW",padx=5,pady=2)
e1.config(xscrollcommand=h_scroll.set)
root.mainloop()
We set up an Entry box named 'e1'. We also set up a scrollbar named 'h_scroll' which has a command for 'e1.xview'. The Entry box only scrolls on the x-axis. The Entry box has an 'xscrollcommand' set to the scrollbar's horizontal scroll, 'h_scroll'. So the two are now linked for scrolling horizontally. More details will be given in the article for the 'Scrollbar'.
Cursor
If you move the cursor over an Entry box, you can change the cursor that is seen by a user. You can look at the article for Labels (Python 17 - Labels) to see the list. Use one of these cursor types is used for a button, the cursor reverts when it is no longer over the button.
An example is to make the cursor a 'boat' when it moves over the button:
Code:
b1=Entry(root,cursor='boat')
Validate, ValidateCommand and InvalidCommand
When data is entered in an Entry box, we can set up a 'validate' event to occur when the event happens. There are six validation events:
- focus - when Entry gets or loses focus
- focusin - when Entry gets focus
- focusout - when Entry loses focus
- key - when there is a keystroke in the Entry
- all - if the Entry has a focusin, focusout or key event
- none - turns validation off (default)
A quick example is as follows:
Code:
from tkinter import *
def validate_entry(new_value):
if new_value.isdigit() or new_value == "":
l1.config(text="True")
return True
else:
l1.config(text="False")
return False
root = Tk()
root.title("Entry Validation")
root.geometry("400x500")
vcmd = (root.register(validate_entry), '%P')
l1=Label(text="")
l1.grid(row=3,column=1)
e1 = Entry(root, validate='key', validatecommand=vcmd)
e1.grid(row=0,column=1)
e1.focus_force()
root.mainloop()
So, we create an Entry, 'e1', and a label, 'l1'. The Entry gets focus when the code is executed. When setting up the Entry box, we set the 'validatecommand' to 'vcmd' when there is a 'key' event. In turn, 'vcmd' is set to running 'root.register' with the function 'validate_entry' and a parameter of '%P'. These parameters are as follows:
- %d - code of 0 for attempted deletion, 1 for attempted insertion or -1 for focus in, focus out or change of textvariable
- %i - index of the insertion or deletion point, -1 for focus in or focus out or change to textvariable
- %P - value of the text entered in the Entry box
- %s - text in the Entry box before the change
- %S - if the event is an insertion or deletion of text, the value is the text being inserted or deleted
- %v - value of the validate option
- %V - reason for the event: 'focusin', 'focusout', 'key', or 'forced' if textvariable was changed
- %W - name of the widget
If you wanted to check for 'invalidcommand', the code would be:
Code:
ivcmd = (self.register(self.on_invalid),)
e1.config(validate='key', validatecommand=vcmd, invalidcommand=ivcmd)
These work the same way as the 'validatecommand', but just occur when it is invalid.
Change the event parameter as you need for the function to work correctly. Notice too that the key pressed is only placed into the Entry box when the 'validationcommand' returns TRUE.
Conclusion
There have been a lot of details covered in both of these articles. There are a lot of options that can be used for the Entry box.
If you plan on learning Python, even just to make minor programs, the Entry box can be a major widget used regularly. Look over these two carefully.

