Updated March 28, 2023
Introduction to Python Tkinter Entry
Python Tkinter entry is among the most generally used graphical user interface in python to design textboxes in GUIs. Here the entry widget in tkinter is a user for building customizable textboxes in the GUI interfaces which are being developed.
Syntax:
Entry(master,option=value,)
Attributes
Following are commonly used attributes:
Option | Description |
bg | Used for setting the background color of the text box |
command | The operation which needs to happen when the state of this textbox is changed |
exportselection | Setting the export selection to zero restricts the automatic selection of the text box to the clipboard |
fg | Color for the text in the textbox |
highlightcolor | Color for highlightinig when the cursor is over |
justify | Justify the textbox content when extended to multiple lines |
show | Used when some sought of special character has to be displayed in the textbox screen, this special character actually takes place in the position of the actual text |
textvariable | This option should be set with the value StringVar when the text in the textbox is expected to get retrieved |
width | Defines the width of the textbox |
xscrollcommand | Allows to attach a scrollbar to the textbox |
Methods
Following are commonly used methods:
Option | Description |
delete(first,last) | Used for deleting the contents of the textbox |
get() | the current text in the textbox will be returned as a string |
insert() | The insert method is used to insert the given text in the textbox |
select_adjust ( index ) | Allows to select characters at a specific index |
select_from ( index ) | Sets the index position to anchor the selection of index |
Examples of Python Tkinter Entry
The examples are given below:
Example #1
Code:
import tkinter as tk
from tkinter import messagebox
obj1 = tk.Tk()
def obj1_clear():
textbox.delete(0,10)
def message_box():
messagebox.showinfo("Yes",textbox.get())
textbox = tk.Entry(obj1,
bg = "yellow",
cursor ="arrow",
exportselection=0,
fg="blue",
highlightcolor="black",
justify="right",
textvariable="StringVar",
width=10,
xscrollcommand="scrollbar"
)
textbox.pack()
clear_button = tk.Button(obj1,
text = "clear",
command = obj1_clear
)
redisplay_button = tk.Button(obj1,
text = "Redisplay",
command = message_box
)
clear_button.pack()
redisplay_button.pack()
obj1.mainloop()
Output:
Explanation:
As the entry widget in Tkinter is used for designing a textbox, here in this example, we have used this option to design a textbox with two action buttons associated with it. One is the clear button used for erasing the textbox’s content, and the other one is the redisplay button, which generates a message prompt holding the value entered in the textbox.
The textbox is created with the below-mentioned attributes,
- background: yellow
- cursor: arrow
- export selection: 0
- foreground: blue
- highlight color: black
- justify: right
- text variable: StringVar
- width: 10
- scroll command: scrollbar
The Tkinter button function is used for creating the necessary buttons. Here, the first button’s variable name is placed as clear_button, and the second button is placed as redisplay_button. Both the buttons are generated using object1 as the control object. The generated text boxes and the buttons are packed using the pack() method. packing these entities make them be a part of the frame or the panel to which it is associated. the command line of the redisplay button is associated with a method called message box. The intention of the message box method is to prompt a Tkinter message box when triggered by the redisplay_button. the message box is built in such a manner that
it will retrieve content from the textbox which was created before. Setting this particular method with the textbox variable in terms like “textbox.get()” will help to retrieve the contents of the textbox and post it as a part of the message box prompt. Another notable point is for generating the message box the “from Tkinter import message box” the quoted import is applied.
On execution of the given program, we can notice that the output was text value typed in the text box and is pulled and populated as a standard content of the message box prompt.
Example #2
Code:
import tkinter as tk
obj1 = tk.Tk()
obj2 = tk.Tk()
def obj1_clear():
textbox1.delete(0,10)
def fill_textbox2():
textbox2.insert(1,textbox1.get())
textbox1 = tk.Entry(obj1,
bg = "yellow",
cursor ="arrow",
fg="blue",
highlightcolor="black",
justify="right",
textvariable="StringVar",
width=100,
xscrollcommand="scrollbar"
)
textbox1.pack()
clear_button = tk.Button(obj1,
text = "clear",
command = obj1_clear
)
clear_button.pack()
textbox2 = tk.Entry(obj2,
bg = "yellow",
cursor ="arrow",
fg="blue",
highlightcolor="black",
justify="right",
textvariable="StringVar",
width=50,
xscrollcommand="scrollbar"
)
textbox2.pack()
redisplay_button = tk.Button(obj1,
text = "Redisplay",
command = fill_textbox2
)
redisplay_button.pack()
obj1.mainloop()
obj2.mainloop()
Output:
Explanation:
This is another example that embraces the application of tkinter entry method for creating and using a customizable textbox setup. Again in this example, the primary panel is associated with two buttons; one is the clear button, and the other one is the redisplay button. In this example, instead of pulling the value from a textbox and posting it in a message prompt, here we have pulled the value from the message box and posted it into another customizable textbox widget.
Here both the textboxes are created with the below-mentioned attributes,
- background: yellow
- cursor: arrow
- foreground: blue
- highlight color: black
- justify: right
- text variable: StringVar
- width: 50
- scroll command: scrollbar
Here, the Tkinter button function is used for creating the needed button widgets on discussing the coding terms. Also, the names or variable names of the button used here are very much similar to the above example, it is as clear_button and redisplay_button. Also, here both the buttons are again generated using object1 as the control object. The generated text boxes and the buttons are packed using the pack() method. packing these entities make them be a part of the frame or the panel to which it is associated.
Here the additional text box was the content of the first textbox will be redisplayed is declared as a separate widget in terms of the control object to which it is associated. here a separate object2 is declared and placed as the control for the second textbox. Again, the retrieval of the content from the first textbox is achieved through the command argument in the button widget. More specifically to mention the “textbox1.get()” is used for pulling the value from the primary textbox(textbox1), and textbox2.insert() is used for inserting the retrieved value in the second text.
Again here on the execution of the program, we can notice the output was text value which has been typed in the primary text box is pulled and populated in the other textbox(textbox2).
Conclusion
The above programs help us to achieve deep insight into Tkinter entry widgets and the sophisticated manner of using them.
Recommended Articles
This is a guide to Python Tkinter Entry. Here we discuss the introduction, attributes, methods, and examples of Python Tkinter Entry along with Outputs. You can also go through our other related articles to learn more–