Tkinter is a standard GUI (Graphical User Interface) toolkit for Python. It provides a set of GUI widgets or controls to create desktop applications.
The Tkinter Entry widget is one such widget that allows the user to enter text or numbers in a single line.
The Entry widget is used to accept single-line text strings from a user. It is a rectangular box where the user can type in the required text or number. You can use it to create a simple text input form, a search bar, or a login form, among others.
from tkinter import * root = Tk() entry = Entry(root, options) entry.pack() root.mainloop()
Here, the Entry() method creates an Entry widget.
The root parameter specifies the parent window in which the Entry widget will be placed.
The options parameter can be used to set various options for the widget, such as its size, color, font, etc.
Once you have created the Entry widget, you can add it to your window using the pack() method, which adds the widget to the window and automatically arranges it based on the available space.
You can get the text entered by the user using the get() method of the Entry widget. For example:
text = entry.get()
This will return the text entered by the user in the Entry widget. You can use this text in your program as required.
Overall, the Tkinter Entry widget is a simple yet powerful way to accept user input in your Python GUI applications.
Here is a list of some of the most commonly used options for the Tkinter Entry widget:
Specifies the width of the Entry widget in characters. For example, width=20 will create an Entry widget with a width of 20 characters.
Specifies the foreground color (i.e., the text color) of the Entry widget. For example, fg=’red’ will make the text in the Entry widget red.
Specifies the background color of the Entry widget. For example, bg=’white’ will make the background of the Entry widget white.
Specifies the font of the text in the Entry widget. For example, font=(‘Arial’, 14) will set the font to Arial with a size of 14 points.
Specifies the horizontal alignment of the text in the Entry widget. For example, justify=CENTER will center the text within the widget. Other possible values are LEFT (the default), RIGHT, and CENTER.
Specifies a character to display instead of the actual characters entered by the user. This is often used for password fields, where the characters entered by the user should not be visible. For example, show=’*’ will display an asterisk (*) for each character entered by the user.
Specifies whether the Entry widget is editable or not. For example, state=DISABLED will make the Entry widget read-only.
Specifies a validation function to be called when the Entry widget is validated.
For example:
validatecommand=root.register(my_validation_function)
will register the my_validation_function as the validation function for the widget.
Specifies a function to be called if the validation fails.
For example,
invalidcommand=my_invalid_function
will call my_invalid_function if the validation fails.
Specifies the border width of the Entry widget.
Specifies the cursor type when the mouse pointer is over the Entry widget.
Specifies whether the selected text should be exported to the clipboard. The default value is True.
Specifies the color of the highlight when the Entry widget is not focused.
Specifies the color of the highlight when the Entry widget is focused.
Specifies the thickness of the highlight when the Entry widget is focused.
Specifies the color of the insertion cursor.
Specifies the width of the insertion cursor.
Specifies the time (in milliseconds) to wait before hiding the insertion cursor. The default value is 300.
Specifies the time (in milliseconds) to wait before showing the insertion cursor.
The default value is 600.
Specifies the width of the insertion cursor.
Specifies the background color of a read-only Entry widget.
Specifies the color of the selected text.
Specifies the width of the selection highlight.
Specifies the color of the selected text.
Specifies whether the Entry widget can receive the focus or not.
Specifies a Tkinter variable to be linked to the text in the Entry widget.
Specifies when the Entry widget should be validated.
Specifies a validation function to be called when the Entry widget is validated.
Specifies a function to be called when the horizontal scrollbar is moved.
Note that not all of these options are applicable to every situation, and some may have different default values depending on the platform you are using.
However, these are the options you can choose from when creating and customizing a Tkinter Entry widget.
Here are some code examples that demonstrate each option for the Tkinter Entry widget:
import tkinter as tk root = tk.Tk() # bd - border width entry_bd = tk.Entry(root, bd=3) entry_bd.pack() # bg - background color entry_bg = tk.Entry(root, bg='light gray') entry_bg.pack() # cursor - cursor type entry_cursor = tk.Entry(root, cursor='pirate') entry_cursor.pack() # exportselection - export selection to clipboard entry_export = tk.Entry(root, exportselection=False) entry_export.pack() # font - text font entry_font = tk.Entry(root, font=('Helvetica', 16)) entry_font.pack() # fg - text color entry_fg = tk.Entry(root, fg='blue') entry_fg.pack() # highlightbackground - highlight color when not focused entry_highlightbg = tk.Entry(root, highlightbackground='red') entry_highlightbg.pack() # highlightcolor - highlight color when focused entry_highlightcolor = tk.Entry(root, highlightcolor='blue') entry_highlightcolor.pack() # highlightthickness - highlight thickness when focused entry_highlightthickness = tk.Entry(root, highlightthickness=2) entry_highlightthickness.pack() # insertbackground - insertion cursor color entry_insertbg = tk.Entry(root, insertbackground='green') entry_insertbg.pack() # insertborderwidth - insertion cursor width entry_insertbw = tk.Entry(root, insertborderwidth=2) entry_insertbw.pack() # insertofftime - time to wait before hiding the insertion cursor entry_insertoff = tk.Entry(root, insertofftime=1000) entry_insertoff.pack() # insertontime - time to wait before showing the insertion cursor entry_inserton = tk.Entry(root, insertontime=200) entry_inserton.pack() # insertwidth - insertion cursor width entry_insertwidth = tk.Entry(root, insertwidth=4) entry_insertwidth.pack() # invalidcommand - function to call if validation fails def invalid_command(): print("Invalid entry") entry_invalid = tk.Entry(root, validate='focusout', invalidcommand=invalid_command) entry_invalid.pack() # justify - text justification entry_justify = tk.Entry(root, justify='center') entry_justify.pack() # readonlybackground - background color of readonly entry entry_readonlybg = tk.Entry(root, readonlybackground='gray') entry_readonlybg.configure(state='readonly') entry_readonlybg.pack() # selectbackground - selected text background color entry_selectbg = tk.Entry(root, selectbackground='yellow') entry_selectbg.pack() # selectborderwidth - selected text border width entry_selectbw = tk.Entry(root, selectborderwidth=2) entry_selectbw.pack() # selectforeground - selected text color entry_selectfg = tk.Entry(root, selectforeground='black') entry_selectfg.pack() # show - character to display instead of actual text entry_show = tk.Entry(root, show='*') entry_show.pack() # state - readonly or normal state entry_state = tk.Entry(root, state='readonly') entry_state.pack() # takefocus - focusable or not entry_focus = tk.Entry(root, takefocus=False) entry_focus.pack() # textvariable - link variable to entry text entry_textvar = tk.StringVar() entry_text = tk.Entry(root, textvariable=entry_textvar) entry_text.pack() # validate - when to validate entry entry_validate = tk.Entry(root, validate='key') entry_validate.pack() # validatecommand - function to validate entry def validate_command(): print("Valid entry") entry_validatecommand = tk.Entry(root, validate='focusout', validatecommand=validate_command) entry_validatecommand.pack() root.mainloop()
Here are some common methods of the Tkinter Entry widget with code examples:
Returns the current text value of the Entry widget.
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def print_entry_text(): text = entry.get() print(text) button = tk.Button(root, text="Print Entry Text", command=print_entry_text) button.pack() root.mainloop()
Deletes characters from the Entry widget. Takes two arguments: starting index and ending index (ending index not inclusive).
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def delete_text(): entry.delete(0, 3) button = tk.Button(root, text="Delete Text", command=delete_text) button.pack() root.mainloop()
Inserts text into the Entry widget at the specified index.
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def insert_text(): entry.insert(0, "Hello, World!") button = tk.Button(root, text="Insert Text", command=insert_text) button.pack() root.mainloop()
Sets the insertion cursor at the specified index.
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def set_cursor(): entry.icursor(5) button = tk.Button(root, text="Set Cursor", command=set_cursor) button.pack() root.mainloop()
Returns the numerical index corresponding to the given string index.
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def get_index(): index = entry.index(tk.END) print(index) button = tk.Button(root, text="Get Index", command=get_index) button.pack() root.mainloop()
Selects the characters between the given starting index and ending index (ending index not inclusive).
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def select_text(): entry.select_range(0, 5) button = tk.Button(root, text="Select Text", command=select_text) button.pack() root.mainloop()
Deselects any selected text in the Entry widget.
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() def clear_selection(): entry.select_clear() button = tk.Button(root, text="Clear Selection", command=clear_selection) button.pack() root.mainloop()
References of this lesson
Python GUI Programming Recipes using PyQt5 by B.M. Harwani
Python GUI Programming Cookbook by Burkhard Meier
Tkinter GUI Programming by Example by David Love