Entry Widget in Tkinter (GUI Programming) – Python Tkinter Tutorial

Python has vast number of GUI libraries which can be used for developing GUI applications in an easy way. All widgets like labels, buttons etc are pre-defined. So, we just have to call their function and implement in our application.

python-video-tutorials

Entry: The entry widget is also one of the widget which is pre-defined in python’s tkinter library. This widget is used for taking values from user and for other purposes based on application requirements. We can also customize this widget by using its pre-defined attributes ie. fg, bg etc.

Syntax: 

#Syntax
entry = Entry(root, options)
entry.pack()

Let’s have an example to understand it in a better way:-

Example 1:

# Entry Widget in GUI python
from tkinter import *

# creating root window
root = Tk()
root.title("Welcome to Python Lobby")

# placing Entry widget to our GUI app
entry = Entry(root)
entry.pack()

root.geometry('250x200')
root.mainloop()

Output:
entry-widget-in-python

gui-in-python

Explanation:

Step 1: We imported tkinter package using “import” keyword.

Step 2: We created an object “root” variable to call Tk() function, which is pre-defined in tkinter library responsible for executing code for creating a small GUI window.

Step 3: Next we have used same root object to customize the title of our GUI application by passing string value as an argument.

Step 4: Next we have defined entry object and called Entry() function by passing root as argument. Here root act as the window or frame where we want to place that entry widget.

Important: “entry.pack()” it is a type of geometry manager that we will discuss later in our upcoming articles.

Step 5: Call mainloop() function to finally execute our loop.

Attributes used with Entry Widget

Attribute Name Descriptions
bg It is used for the background color displayed behind the widget.
bd It is used for the size of border. By default size is 2px.
font It is used for customizing the default font properties.
fg It is used for text color customization.
justify It is for the text alignment. RIGHT, CENTER, LEFT AND JUSTIFY
relief It is basically the style of our entry widget etc.
show It is used for implementing limitations on the text shown in the entry widget ie. for password protect we use setshow=”*”
textvariable It is used for getting the current text in the widget.

 

Example 2: We will implement some attributes in this example:-

# Entry Widget example 
from tkinter import *

# creating root window
root = Tk()
root.title("Welcome to Python Lobby")

# placing Entry widget to our GUI app
entry = Entry(root, bg="yellow", fg="red", bd=5)
entry.pack()

root.geometry('250x200')
root.mainloop()

#

Output:
entry-widget-in-python

gui-in-python

Explanation:

Step 1: We imported tkinter package using “import” keyword.

Step 2: We created an object “root” variable to call Tk() function, which is pre-defined in tkinter library responsible for executing code for creating a small GUI window.

Step 3: Next we have used same root object to customize the title of our GUI application by passing string value as an argument.

Step 4: Next we have defined entry object and called Entry() function by passing root as argument. Here root act as the window or frame where we want to place that entry widget.

Note:  Attributes used:

  • bg = it is used to change the background color .
  • fg = it is used to change the foreground color ie. font color.
  • bd = it is used to change the width of border, by default width is 2px.

Important: “entry.pack()” it is a type of geometry manager that we will discuss later in our upcoming articles.

Step 5: Call mainloop() function to finally execute our loop.