When programming, sometimes you will want to let the user enter or change text. You can use the Entry widget just for that purpose. Keep in mind that only one line of text can be in the Entry object.
For most programs, this can be a very important widget to use.
Before we get to changing the 'look' of the entry box and its contents, we need to understand the methods used to manage the information in the Entry object.
Creating an Entry
Creating an Entry object is the same as creating a Button or Label, just use the word 'Entry':
We now have an Entry box that is accessed by 'e1' or whatever name you give it. The box will be empty. You can pass the option of 'root' or the name of a window you create:
Inserting Text
The Entry object is used for entering and changing text. By creating an empty object, we can let the user enter text. But what if we want to change existing text?
We can insert text into the Entry box to let the user change it:
The object needs to be created before you can insert text into it. The '0' is an index or counter. We can insert text into an Entry box that already has text in it. The index value is the position where you place the new text. The first character in the Entry box is '0' and counts up. For example, we can insert text and then insert more at a different place:
The resulting text will be 'Entry 1 Box'. We insert a space and '1' at index 5, which is the end of the word 'Entry'. Just count the index numbers as the point right before each character.
If the index is more than the characters that exist in the text, then it will just be put at the end of the existing text.
Deleting Text
You may want to remove text from the Entry box. This is done with the 'delete' method. It takes two parameters. The first parameter is required and specifies the first character to delete. This parameter is the index of the character. The second parameter is where to stop deleting text and is optional. If you omit the second parameter, then only one character is deleted at the point referenced by the starting index.
Let's look at the previous example and then delete the characters from index 0 to 6. Remember that the second insertion placed it at index 5, but the insertion text included a space, so we need to remove the text from index 0 to 6:
The remaining text in the Entry box is '1 Box'.
You can delete all the text in the Entry box by specifying '0' as the start and 'END' as the second parameter.
If a starting and ending value are given, that is beyond the number of characters, then nothing is deleted.
Getting the Text
Once text is in the Entry box, we need to retrieve the input from the user. So, to get the text from the Entry box, we use the 'get' method.
Let's assume we have the entry and need to get the user's input and place it in the variable 'user_input':
Here, we have an Entry box and a Button that, when pressed, will take the text in the Entry box and place it into the 'user_input' variable in the function 'accept'. The data in the variable 'user_input' is then placed into the Label, named 'l1'. This method to get the data is 'user_input=e1.get()'. The data is retrieved from Entry titled 'e1' and placed into 'user_input'.
Insertion Cursor
When text has been entered or is being changed, then the user can click and move the insertion cursor. But we can programmatically move the insertion cursor to a specific spot.
If the user enters text into the Entry box, we can check the text and then, if there is an error, we can move the insertion cursor to the point of the error.
For example, let's say we ask for the input of a phone number and we find a non-numeric character. So, let's say the user enters '555-555-5s55'. So the index is at '9' for the letter. To cause the insertion cursor to be moved there, we use the code:
Notice that for the insertion cursor to be set, the Entry box needs to have the focus.
XView
Sometimes the text in the Entry box is longer than the visible length of the box. Some of the text will be outside of the box and not visible. We can move the text to allow for viewing of the non-visible text.
To do this, we set the insertion point where we want to view. Then we call the method 'xview' with the same index:
The index places the insertion point right after 'z' and before '0', as well as moving the text to make the end visible.
If the index is lower, such as '7', then the 8th character is at the far left of the Entry box. Some of the text is visible, but the far left and right are outside the box and not visible.
Xview_moveto
If the text in the Entry box exceeds the visible number of characters, we can move the visibility to the left or right side of the text.
The parameters used for this method are '0' for the left side and '1' for the right side. If you place an insertion cursor before you use 'moveto', it will not affect the position of the text set by 'moveto'.
For example, if I want the text in the box to be all the way to the right, then the code is:
XView_scroll
Here, we can scroll in 'units' or 'pages'. A unit is one character, and a page is 19 characters. There is a number given that represents how many 'units' or 'pages' to move.
A negative number is to the right, while a positive number is to the left. I made an example, but I doubled the length of the Entry text:
Each button will move to the left or right a single 'unit' or 'page'.
Index
You can find the current insertion point by getting its index. The constant to use is 'INSERT'. There are four constants that can be used:
The variable 'place' should contain the value of '30'.
If I use the constant 'END', it should get a value of '72'.
Select_preset
In this method, we can check whether there is anything selected in the Entry box.
We check by checking the method '.select_preset' is TRUE or FALSE:
Here, we test the selection in the Entry box after a button is pressed. We then check whether the Entry has a selection or not and then puts test in a Label.
If there is a selection of text, then we can use the 'index' method to get the start and ending indices of the selection.
SelectClear
If we find there is a text selection, we can clear it out.
SelectFrom and SelectTo
We can set an index to use as an 'anchor' point. If we select the 'from' index, it will make this an anchor for starting.
If we then specify a 'to' point, the text specified between 'selectfrom' and 'selectto' will be selected.
An insertion point is made at index 15, or the 'p' character. We then set the selection to go '5' characters from the 'from' point', up to 't'.
SelectRange
Instead of specifying a starting point and ending point separately, we can use one command.
Here, we specify a start and end point.
The line would select text from the index from 20 to 25.
We can use constants here as well:
SelectAdjust
If a selection exists, we may want to make sure that a specific index is included in the selection. For this we use the method '.selectadjust()'.
Inside the parentheses, we can include the index that must be included. If the index is included in the selection, then nothing changes. If the adjusted index is not in the selection, then the selection is increased to include the new index. Again, most constants can be used here.
Here, we set the selection from the 'INSERT' point of '11' to '20'. We adjust that to include the character at index '25'. We then get the selection and place it in a Label, named 'l1'.
ScanMark and ScanDragTo
When you use the mouse to change focus to an Entry box, you can then use the mouse to select text and move it to the far left or right to scroll the text.
With the '.scan_mark()' method, you will choose the first anchor. With '.scandragto()', you can press button 1 on the mouse and drag the selection, but the text will scroll easier.
Try the code:
You should see that it scrolls differently, but easier.
Now this is the end of the methods for Entry and we can get on to option. Most of these we've covered in the Label and Button articles. I may go over them quickly, if you need more details or examples, check the other articles.
Background (bg) and SelectBackground
Using the background option, which can be shortened to 'bg', allows you to change the background color of the text area in the Entry box.
As with the other widgets, you can use color names or RGB numeric values.
The same is true for the 'selectbackground' for specifying a color. The 'selectbackground' is the background color of selected text.
It is advisable that these colors not be the same or a user cannot tell what has been selected.
Width
You can set the width of the Entry box; the default is 20 if the default is not enough. The value you specify is the number of characters that can be entered into the Entry box before it starts scrolling.
Where is no height option since an Entry box, by definition, is a single line of text.
We can change the width to 30:
Borderwidth (bd) and SelectBorderwidth
The 'borderwidth', which can also be shortened to 'bd', is the extra area around the text. The default is usually two pixels.
If you use the 'selectborderwidth', this is the area around the selected text. The default is one. If the 'selectborderwidth' is more than the 'borderwidth', the box is not enlarged to handle it. There is an extra highlight to the right of the selected text.
Foreground (fg) and SelectForeground
Similar to 'background' and 'selectbackground', these are not the colors of the background, but the foreground text. So, here you can change the foreground color of the text in the Entry box or the text color that is selected.
You can use the color names or the RGB color codes.
It is possible to abbreviate 'foreground' to 'fg'.
Conclusion
Here would be a good stopping point for Part 1.
Look over the examples and make sure you have a good understanding of them before you go on to Part 2.
For most programs, this can be a very important widget to use.
Before we get to changing the 'look' of the entry box and its contents, we need to understand the methods used to manage the information in the Entry object.
Creating an Entry
Creating an Entry object is the same as creating a Button or Label, just use the word 'Entry':
Code:
e1=Entry()
We now have an Entry box that is accessed by 'e1' or whatever name you give it. The box will be empty. You can pass the option of 'root' or the name of a window you create:
Code:
e1=Entry(root)
Inserting Text
The Entry object is used for entering and changing text. By creating an empty object, we can let the user enter text. But what if we want to change existing text?
We can insert text into the Entry box to let the user change it:
Code:
e1.insert(0,"Text to be inserted.")
The object needs to be created before you can insert text into it. The '0' is an index or counter. We can insert text into an Entry box that already has text in it. The index value is the position where you place the new text. The first character in the Entry box is '0' and counts up. For example, we can insert text and then insert more at a different place:
Code:
e1.insert(0,"Entry Box")
e1.insert(5," 1")
The resulting text will be 'Entry 1 Box'. We insert a space and '1' at index 5, which is the end of the word 'Entry'. Just count the index numbers as the point right before each character.
If the index is more than the characters that exist in the text, then it will just be put at the end of the existing text.
Deleting Text
You may want to remove text from the Entry box. This is done with the 'delete' method. It takes two parameters. The first parameter is required and specifies the first character to delete. This parameter is the index of the character. The second parameter is where to stop deleting text and is optional. If you omit the second parameter, then only one character is deleted at the point referenced by the starting index.
Let's look at the previous example and then delete the characters from index 0 to 6. Remember that the second insertion placed it at index 5, but the insertion text included a space, so we need to remove the text from index 0 to 6:
Code:
e1.insert(0,"Entry Box")
e1.insert(5," 1")
e1.delete(0,5)
The remaining text in the Entry box is '1 Box'.
You can delete all the text in the Entry box by specifying '0' as the start and 'END' as the second parameter.
If a starting and ending value are given, that is beyond the number of characters, then nothing is deleted.
Getting the Text
Once text is in the Entry box, we need to retrieve the input from the user. So, to get the text from the Entry box, we use the 'get' method.
Let's assume we have the entry and need to get the user's input and place it in the variable 'user_input':
Code:
from tkinter import *
def accept():
user_input=e1.get()
l1.configure(text=user_input)
root = Tk()
root.title('Entry Base')
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)
user_input=''
e1=Entry()
b1=Button(text="Accept",command=accept)
l1=Label()
b1.grid(column=0, row=1)
e1.grid(column=0, row=0)
l1.grid(column=0, row=3)
root.mainloop()
Here, we have an Entry box and a Button that, when pressed, will take the text in the Entry box and place it into the 'user_input' variable in the function 'accept'. The data in the variable 'user_input' is then placed into the Label, named 'l1'. This method to get the data is 'user_input=e1.get()'. The data is retrieved from Entry titled 'e1' and placed into 'user_input'.
Insertion Cursor
When text has been entered or is being changed, then the user can click and move the insertion cursor. But we can programmatically move the insertion cursor to a specific spot.
If the user enters text into the Entry box, we can check the text and then, if there is an error, we can move the insertion cursor to the point of the error.
For example, let's say we ask for the input of a phone number and we find a non-numeric character. So, let's say the user enters '555-555-5s55'. So the index is at '9' for the letter. To cause the insertion cursor to be moved there, we use the code:
Code:
e1.insert(0,"555-555-5s55")
e1.focus()
e1.icursor(9)
Notice that for the insertion cursor to be set, the Entry box needs to have the focus.
XView
Sometimes the text in the Entry box is longer than the visible length of the box. Some of the text will be outside of the box and not visible. We can move the text to allow for viewing of the non-visible text.
To do this, we set the insertion point where we want to view. Then we call the method 'xview' with the same index:
Code:
e1.focus()
e1.insert(0,"abcdefghijklmnopqrstuvwxyz0123456789")
e1.icursor(26)
e1.xview(26)
The index places the insertion point right after 'z' and before '0', as well as moving the text to make the end visible.
If the index is lower, such as '7', then the 8th character is at the far left of the Entry box. Some of the text is visible, but the far left and right are outside the box and not visible.
Xview_moveto
If the text in the Entry box exceeds the visible number of characters, we can move the visibility to the left or right side of the text.
The parameters used for this method are '0' for the left side and '1' for the right side. If you place an insertion cursor before you use 'moveto', it will not affect the position of the text set by 'moveto'.
For example, if I want the text in the box to be all the way to the right, then the code is:
Code:
e1.xview_moveto(1)
XView_scroll
Here, we can scroll in 'units' or 'pages'. A unit is one character, and a page is 19 characters. There is a number given that represents how many 'units' or 'pages' to move.
A negative number is to the right, while a positive number is to the left. I made an example, but I doubled the length of the Entry text:
Code:
from tkinter import *
def left_unit():
e1.xview_scroll(1,"units")
def right_unit():
e1.xview_scroll(-1,"units")
def left_page():
e1.xview_scroll(1,"pages")
def right_page():
e1.xview_scroll(-1,"pages")
root = Tk()
root.title('Entry Index')
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=20)
b1=Button(text="< Unit",command=left_unit)
b2=Button(text="<< Page",command=left_page)
b3=Button(text="Unit >",command=right_unit)
b4=Button(text="Page >>",command=right_page)
e1.focus()
e1.insert(0,"abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789")
b1.grid(column=0, row=1)
b2.grid(column=0, row=2)
b3.grid(column=1, row=1)
b4.grid(column=1, row=2)
e1.grid(column=0, row=0)
root.mainloop()
Each button will move to the left or right a single 'unit' or 'page'.
Index
You can find the current insertion point by getting its index. The constant to use is 'INSERT'. There are four constants that can be used:
- INSERT - current index of insertion cursor
- END - index of the end of the text
- SELFIRST - index of first selected characters
- SELLAST - index of last selected characters
Code:
e1.focus()
e1.insert(0,"abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789")
e1.icursor(30)
place=e1.index(INSERT)
The variable 'place' should contain the value of '30'.
If I use the constant 'END', it should get a value of '72'.
Select_preset
In this method, we can check whether there is anything selected in the Entry box.
We check by checking the method '.select_preset' is TRUE or FALSE:
Code:
def check():
if e1.select_present()==TRUE:
l1.configure(text="True")
if e1.select_present()==FALSE:
l1.configure(text="False")
Here, we test the selection in the Entry box after a button is pressed. We then check whether the Entry has a selection or not and then puts test in a Label.
If there is a selection of text, then we can use the 'index' method to get the start and ending indices of the selection.
SelectClear
If we find there is a text selection, we can clear it out.
Code:
def check():
if e1.select_present()==TRUE:
e1.select_clear()
SelectFrom and SelectTo
We can set an index to use as an 'anchor' point. If we select the 'from' index, it will make this an anchor for starting.
If we then specify a 'to' point, the text specified between 'selectfrom' and 'selectto' will be selected.
Code:
e1.insert(0,"abcdefghijklmnopqrstuvwxyz0123456789")
e1.icursor(15)
place=e1.index(INSERT)
e1.select_from(place)
e1.select_to(place + 5)
An insertion point is made at index 15, or the 'p' character. We then set the selection to go '5' characters from the 'from' point', up to 't'.
SelectRange
Instead of specifying a starting point and ending point separately, we can use one command.
Here, we specify a start and end point.
Code:
e1.selectrange(20,25)
The line would select text from the index from 20 to 25.
We can use constants here as well:
- INSERT - current index of insertion cursor
- ANCHOR - starting point set by 'selectfrom'
- END - end of the text in the Entry box
SelectAdjust
If a selection exists, we may want to make sure that a specific index is included in the selection. For this we use the method '.selectadjust()'.
Inside the parentheses, we can include the index that must be included. If the index is included in the selection, then nothing changes. If the adjusted index is not in the selection, then the selection is increased to include the new index. Again, most constants can be used here.
Code:
e1.insert(0,"abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz")
e1.icursor(11)
place=e1.index(INSERT)
e1.select_from(place)
e1.select_range(INSERT,20)
e1.select_adjust(25)
l1.configure(text=e1.selection_get())
Here, we set the selection from the 'INSERT' point of '11' to '20'. We adjust that to include the character at index '25'. We then get the selection and place it in a Label, named 'l1'.
ScanMark and ScanDragTo
When you use the mouse to change focus to an Entry box, you can then use the mouse to select text and move it to the far left or right to scroll the text.
With the '.scan_mark()' method, you will choose the first anchor. With '.scandragto()', you can press button 1 on the mouse and drag the selection, but the text will scroll easier.
Try the code:
Code:
from tkinter import *
def mouse_down(event):
e1.scan_mark(event.x)
def mouse_move(event):
e1.scan_dragto(event.x)
root = Tk()
root.title("Entry scan_mark")
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=15)
e1.grid(column=0,row=0)
e1.bind('<Button-1>',mouse_down)
e1.bind('<B1-Motion>',mouse_move)
root.mainloop()
You should see that it scrolls differently, but easier.
Now this is the end of the methods for Entry and we can get on to option. Most of these we've covered in the Label and Button articles. I may go over them quickly, if you need more details or examples, check the other articles.
Background (bg) and SelectBackground
Using the background option, which can be shortened to 'bg', allows you to change the background color of the text area in the Entry box.
As with the other widgets, you can use color names or RGB numeric values.
The same is true for the 'selectbackground' for specifying a color. The 'selectbackground' is the background color of selected text.
It is advisable that these colors not be the same or a user cannot tell what has been selected.
Width
You can set the width of the Entry box; the default is 20 if the default is not enough. The value you specify is the number of characters that can be entered into the Entry box before it starts scrolling.
Where is no height option since an Entry box, by definition, is a single line of text.
We can change the width to 30:
Code:
e1=Entry(width=30)
Borderwidth (bd) and SelectBorderwidth
The 'borderwidth', which can also be shortened to 'bd', is the extra area around the text. The default is usually two pixels.
If you use the 'selectborderwidth', this is the area around the selected text. The default is one. If the 'selectborderwidth' is more than the 'borderwidth', the box is not enlarged to handle it. There is an extra highlight to the right of the selected text.
Foreground (fg) and SelectForeground
Similar to 'background' and 'selectbackground', these are not the colors of the background, but the foreground text. So, here you can change the foreground color of the text in the Entry box or the text color that is selected.
You can use the color names or the RGB color codes.
It is possible to abbreviate 'foreground' to 'fg'.
Conclusion
Here would be a good stopping point for Part 1.
Look over the examples and make sure you have a good understanding of them before you go on to Part 2.

