EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Tkinter Tutorial Python Tkinter Entry
 

Python Tkinter Entry

Anandkumar Murugesan
Article byAnandkumar Murugesan
EDUCBA
Reviewed byRavi Rathore

Updated March 28, 2023

Python Tkinter Entry

 

 

Introduction to Python Tkinter Entry

Python Tkinter entry is among the most generally used graphical user interface in python to design textboxes in GUIs. Here the entry widget in tkinter is a user for building customizable textboxes in the GUI interfaces which are being developed.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Entry(master,option=value,)

Attributes

Following are commonly used attributes:

Option Description
bg Used for setting the background color of the text box
command The operation which needs to happen when the state of this textbox is changed
exportselection Setting the export selection to zero restricts the automatic selection of the text box to the clipboard
fg Color for the text in the textbox
highlightcolor Color for highlightinig when the cursor is over
justify Justify the textbox content when extended to multiple lines
show Used when some sought of special character has to be displayed in the textbox screen, this special character actually takes place in the position of the actual text
textvariable This option should be set with the value StringVar when the text in the textbox is expected to get retrieved
width Defines the width of the textbox
xscrollcommand Allows to attach a scrollbar to the textbox

Methods

Following are commonly used methods:

Option Description
delete(first,last) Used for deleting the contents of the textbox
get() the current text in the textbox will be returned as a string
insert() The insert method is used to insert the given text in the textbox
select_adjust ( index ) Allows to select characters at a specific index
select_from ( index ) Sets the index position to anchor the selection of index

Examples of Python Tkinter Entry

The examples are given below:

Example #1

Code:

import tkinter as tk
from tkinter import messagebox
obj1 = tk.Tk()
def obj1_clear():
textbox.delete(0,10)
def message_box():
messagebox.showinfo("Yes",textbox.get())
textbox = tk.Entry(obj1,
          bg = "yellow",
          cursor ="arrow",
          exportselection=0,
          fg="blue",
          highlightcolor="black",
          justify="right",
          textvariable="StringVar",
          width=10,
          xscrollcommand="scrollbar"
          )
textbox.pack()
clear_button = tk.Button(obj1,
               text = "clear",
               command = obj1_clear
               )
redisplay_button = tk.Button(obj1,
          text = "Redisplay",
          command = message_box
           )
clear_button.pack()
redisplay_button.pack()
obj1.mainloop()

Output:

python tkinter entry -example1

Explanation:

As the entry widget in Tkinter is used for designing a textbox, here in this example, we have used this option to design a textbox with two action buttons associated with it. One is the clear button used for erasing the textbox’s content, and the other one is the redisplay button, which generates a message prompt holding the value entered in the textbox.

The textbox is created with the below-mentioned attributes,

  • background: yellow
  • cursor: arrow
  • export selection: 0
  • foreground: blue
  • highlight color: black
  • justify: right
  • text variable: StringVar
  • width: 10
  • scroll command: scrollbar

The Tkinter button function is used for creating the necessary buttons. Here, the first button’s variable name is placed as clear_button, and the second button is placed as redisplay_button. Both the buttons are generated using object1 as the control object. The generated text boxes and the buttons are packed using the pack() method. packing these entities make them be a part of the frame or the panel to which it is associated. the command line of the redisplay button is associated with a method called message box. The intention of the message box method is to prompt a Tkinter message box when triggered by the redisplay_button. the message box is built in such a manner that

it will retrieve content from the textbox which was created before. Setting this particular method with the textbox variable in terms like “textbox.get()” will help to retrieve the contents of the textbox and post it as a part of the message box prompt. Another notable point is for generating the message box the “from Tkinter import message box” the quoted import is applied.

On execution of the given program, we can notice that the output was text value typed in the text box and is pulled and populated as a standard content of the message box prompt.

Example #2

Code:

import tkinter as tk
obj1 = tk.Tk()
obj2 = tk.Tk()
def obj1_clear():
   textbox1.delete(0,10)
def fill_textbox2():
   textbox2.insert(1,textbox1.get())
textbox1 = tk.Entry(obj1,
           bg = "yellow",
           cursor ="arrow",
           fg="blue",
           highlightcolor="black",
           justify="right",
           textvariable="StringVar",
           width=100,
           xscrollcommand="scrollbar"
           )
textbox1.pack()
clear_button = tk.Button(obj1,
        text = "clear",
        command = obj1_clear
         )
clear_button.pack()
textbox2 = tk.Entry(obj2,
           bg = "yellow",
           cursor ="arrow",
           fg="blue",
           highlightcolor="black",
           justify="right",
           textvariable="StringVar",
           width=50,
           xscrollcommand="scrollbar"
           )
textbox2.pack()
redisplay_button = tk.Button(obj1,
             text = "Redisplay",
             command = fill_textbox2
             )
redisplay_button.pack()
obj1.mainloop()
obj2.mainloop()

Output:

python tkinter entry -example2

Explanation:

This is another example that embraces the application of tkinter entry method for creating and using a customizable textbox setup. Again in this example, the primary panel is associated with two buttons; one is the clear button, and the other one is the redisplay button. In this example, instead of pulling the value from a textbox and posting it in a message prompt, here we have pulled the value from the message box and posted it into another customizable textbox widget.

Here both the textboxes are created with the below-mentioned attributes,

  • background: yellow
  • cursor: arrow
  • foreground: blue
  • highlight color: black
  • justify: right
  • text variable: StringVar
  • width: 50
  • scroll command: scrollbar

Here, the Tkinter button function is used for creating the needed button widgets on discussing the coding terms. Also, the names or variable names of the button used here are very much similar to the above example, it is as clear_button and redisplay_button. Also, here both the buttons are again generated using object1 as the control object. The generated text boxes and the buttons are packed using the pack() method. packing these entities make them be a part of the frame or the panel to which it is associated.

Here the additional text box was the content of the first textbox will be redisplayed is declared as a separate widget in terms of the control object to which it is associated. here a separate object2 is declared and placed as the control for the second textbox. Again, the retrieval of the content from the first textbox is achieved through the command argument in the button widget. More specifically to mention the “textbox1.get()” is used for pulling the value from the primary textbox(textbox1), and textbox2.insert() is used for inserting the retrieved value in the second text.

Again here on the execution of the program, we can notice the output was text value which has been typed in the primary text box is pulled and populated in the other textbox(textbox2).

Conclusion

The above programs help us to achieve deep insight into Tkinter entry widgets and the sophisticated manner of using them.

Recommended Articles

This is a guide to Python Tkinter Entry. Here we discuss the introduction, attributes, methods, and examples of Python Tkinter Entry along with Outputs. You can also go through our other related articles to learn more–

  1. If Condition in Python
  2. Split Function in Python
  3. Python Tkinter Button
  4. Python Tkinter Label

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW