In this Python TKinter article we are going to have Complete Guide on Python TKinter Entry Widget, so Tkinter is popular Python GUI (Graphical User Interface) library that comes pre installed with most Python distributions. it provides different widgets that allow developers to create interactive applications. one of the most commonly used widgets in Tkinter is the Entry widget.
What is Python TKinter Entry Widget
Entry widget is used to accept input from the user. it is similar to text box in web form or an input field in a desktop application. the user can type in string of text or number, and the application can retrieve the input and use it in various ways.
Complete Guide on Python TKinter Entry Widget
In this article, we are going to explore Tkinter Entry widget in detail. we will provide examples of how to create Entry widgets using both procedural and object oriented programming (OOP) approaches. we will also discuss some of the most common methods and attributes of Python TKinter Entry widget.
Now let’s create our TKinter Entry Widget, we can then create Entry widget using the Entry() method, passing the parent window as an argument. this is simple example:
1 2 3 4 5 6 7 8 9 |
import tkinter as tk root = tk.Tk() entry = tk.Entry(root) entry.pack() root.mainloop() |
In this code first of all we have imported Tkinter library using the alias tk. after that we have created new Tk() object and assign it to the variable root. this object represents the main window of the application. we then creates new Entry() object and pass the root object as an argument. and finally we call the pack() method to add the widget to the main window and then start the main event loop using the mainloop() method.
Run the complete and this will be the result
Now let’s write the same code using Object Oriented Programming, we still need to import Tkinter library and creates new Tkinter object. however instead of creating Entry widget using standalone method, we creates new class that inherits from the tk.Entry class. this is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import tkinter as tk class MyEntry(tk.Entry): def __init__(self, parent): super().__init__(parent) root = tk.Tk() entry = MyEntry(root) entry.pack() root.mainloop() |
In this code, we creates new class called MyEntry that inherits from tk.Entry class. we then define constructor method that calls the parent constructor using super() function. after that we customize the widget by adding additional methods and attributes to the class.
After that we creates new Tk() object and assign it to the variable root. we creates new MyEntry object and pass the root object as an argument. and finally we call the pack() method to add the widget to the main window, and then start the main event loop using mainloop() method.
Run the code and you will see this result
Common Methods and Attributes of the Entry Widget
Method
- get(): Returns string of text currently entered in the widget.
- delete(first, last=None): Deletes text between specified first and last indices. if last is not specified, only the character at first is deleted.
- insert(index, string): Inserts specified string at the specified index.
- configure(**kwargs): Configures one or more attributes of widget. this method takes dictionary of attribute-value pairs as input.
- focus_set(): Sets focus to the widget and it allows user to type in text without clicking on it.
- select_range(start, end): Selects the text between the specified start and end indices.
- select_clear(): Clears any selected text in the widget.
Attributes
- textvariable: Allows us to associate a tk.StringVar object with the widget. Any text entered into the widget will be stored in the StringVar object, allowing us to easily retrieve and manipulate the input.
- width: Specifies the width of the widget in characters.
- justify: Specifies the alignment of the text within the widget. Valid values are LEFT, CENTER, and RIGHT.
- show: Specifies a character to be displayed in place of the actual text. This is often used for password fields, where the actual characters entered by the user should be obscured.
readonlybackground: Specifies the background color of the widget when it is set to read-only mode. - state: Specifies whether the widget is in normal mode, disabled mode, or read-only mode.
Now that we have learned about basic and fundamental concept of Python TKinter Entry Widget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import tkinter as tk class LoginForm(tk.Frame): def __init__(self, parent): super().__init__(parent) self.username_label = tk.Label(self, text="Username:") self.username_label.grid(row=0, column=0) self.username_entry = tk.Entry(self) self.username_entry.grid(row=0, column=1) self.password_label = tk.Label(self, text="Password:") self.password_label.grid(row=1, column=0) self.password_entry = tk.Entry(self, show="*") self.password_entry.grid(row=1, column=1) self.submit_button = tk.Button(self, text="Submit", command=self.submit) self.submit_button.grid(row=2, column=0, columnspan=2) self.pack() def submit(self): username = self.username_entry.get() password = self.password_entry.get() print(f"Username: {username}\nPassword: {password}") root = tk.Tk() login_form = LoginForm(root) root.mainloop() |
In the above code we have created new class called LoginForm that extends from tk.Frame class. after that we have defined a constructor method that creates two Label widgets and two Entry widgets for the username and password fields. we also creates Button widget for submitting the form.
After that we have defined submit() method that retrieves the text entered into the username and password fields using the get() method. we then print input to the console, but in real application, we would typically use this input to perform some kind of authentication.
And finally we creates new Tk() object and assign it to the variable root. we create new LoginForm object and pass the root in thier.
Run your code and this is the result
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading