The Entry widget represents a text input displayed in a single line. Along with the Label and Button classes, it is one of the most commonly used Tkinter classes.
Creating text entries
How to do it...
This example shows how to create a login form with two entry instances for the username and password fields. Each character of password is displayed as an asterisk to avoid showing it in clear text:
import tkinter as tk class LoginApp(tk.Tk): def __init__(self): super().__init__() self.username = tk.Entry(self) self.password = tk.Entry(self, show="*") self.login_btn = tk.Button(self, text="Log in", command=self.print_login) self.clear_btn = tk.Button(self, text="Clear", command=self.clear_form) self.username.pack() self.password.pack() self.login_btn.pack(fill=tk.BOTH) self.clear_btn.pack(fill=tk.BOTH) def print_login(self): print("Username: {}".format(self.username.get())) print("Password: {}".format(self.password.get())) def clear_form(self): self.username.delete(0, tk.END) self.password.delete(0, tk.END) self.username.focus_set() if __name__ == "__main__": app = LoginApp() app.mainloop()
The Log in button prints the values in the console, whereas the Clear button removes the content of both entries and returns the focus to the entry for username:
How it works...
The Entry widgets are instantiated using the parent window or frame as the first argument and a set of optional keyword arguments to configure additional options. We did not specify any options for the entry corresponding to the username field. To keep the password secret, we specify the show argument with the string "*", which will display each typed character as an asterisk.
With the get() method, we will retrieve the current text as a string. This is used in the print_login method to show the entries' content in the standard output.
The delete() method takes two arguments that indicate the range of the characters that should be deleted. Keep in mind that the indices start at the position 0, and they do not include the character at the end of the range. If only one argument is passed, it deletes the character at that position.
In the clear_form() method, we delete from index 0 to the constant END, which means that the whole content is removed. Finally, we set the focus to the username entry.
There's more...
The content of an Entry widget can be modified programmatically with the insert() method, which takes two arguments:
- index: The position to insert the text; note that entry positions are 0-indexed
- string: The text to insert
A common pattern to reset the content of an entry with a default value can be achieved with a combination of delete() and insert():
entry.delete(0, tk.END) entry.insert(0, "default value")
Another pattern is to append the text in the current position of the text cursor. Here, you can use the INSERT constant instead of having to calculate the numerical index:
entry.insert(tk.INSERT, "cursor here")
Like the Button class, the Entry class also accepts the relief and state options to modify its border style and state. Keep in mind that calls to delete() and insert() are ignored when the state is "disabled" or "readonly".
See also
- The Tracing text changes recipe
- The Validating a text entry recipe