Python 3 – Tkinter Entry
In graphical user interface (GUI) programming, having input fields is essential. In Tkinter, the Entry widget serves as an input box where users can type in information. Here, we will discuss the Entry widget properties, how to create one, and how to manipulate its value.
Properties of Tkinter Entry Widget
In creating Entry widgets, we can customize it by changing the values of its properties. Below are some of the commonly used properties of the Entry widget.
- background – The color of the Entry field
- borderwidth – The width of the entry field border
- cursor – The type of cursor displayed over the entry field
- font – The font style and size of the text in the entry field
- foreground – The color of the text in the entry field
- highlightbackground – The color of the highlight around the entry field when not in focus
- highlightcolor – The color of the highlight around the entry field when in focus
- highlightthickness – The thickness of the highlight around the entry field
- insertbackground – The color of the cursor when not blinking
- insertborderwidth – The width of the cursor border
- insertofftime – The time in milliseconds when the cursor disappears when not blinking
- insertontime – The time in milliseconds when the cursor appears again when not blinking
- insertwidth – The width of the cursor when blinking
- justify – The alignment of the text in the entry field
- show – The character to substitute the text in the entry field for security (e.g., password)
- textvariable – The variable that holds the value of the entry field
- width – The width of the entry field in characters
Creating a Tkinter Entry Widget
To create an Entry widget in Tkinter, we need to import the module and create a Tk object. Then, we can create an Entry object by calling the Entry()
method and passing its parent and other properties as arguments. Finally, we can use the pack()
or grid()
method to add the Entry widget to the parent widget.
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root, width=30, font=('Arial', 14), show='*')
entry.pack()
root.mainloop()
In the code above, we created a Tk object root
, an Entry object entry
, and specified its properties width
(30), font
(‘Arial’, 14), and show
(‘‘). We also used the pack()
method to add the Entry widget to the root widget. When we run the code, it will display a GUI window with an Entry field that can hide the input using the asterisk character (‘‘).
Getting and Setting the Value of a Tkinter Entry Widget
To retrieve the value of an Entry widget, we need to access its text using the get()
method. On the other hand, to set the value of an Entry widget, we can use the insert()
method to insert text or use the delete()
method to remove text from the entry field.
import tkinter as tk
def show_entry():
print(f"The value of the Entry widget is {entry.get()}")
def clear_entry():
entry.delete(0, tk.END)
root = tk.Tk()
entry = tk.Entry(root, width=30, font=('Arial', 14), show='*')
entry.pack()
button_show = tk.Button(root, text="Show Entry", command=show_entry)
button_show.pack()
button_clear = tk.Button(root, text="Clear Entry", command=clear_entry)
button_clear.pack()
root.mainloop()
In the code above, we created two button widgets button_show
and button_clear
. We also defined two functions show_entry()
and clear_entry()
that will print the value of the Entry widget and delete its content, respectively. When we run the code, it will display a GUI window with an Entry field and two buttons. We can type any text on the Entry field, click the Show Entry
button to print its value in the console, and click the Clear Entry
button to remove its content.
Conclusion
The Tkinter Entry widget is a fundamental element in GUI programming, where inputting information from users is necessary. We can customize the appearance of the Entry widget by modifying its properties, create it using the parent widget and the Entry()
method, and manipulate its value using the get()
, insert()
, and delete()
methods.