Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself.
Entry widgets are used to get the entry from the user. It can be created as follows-
entry = ttk.Entry(master, option = value, ...)
Code #1: Creating Entry widget and taking input from user (taking only String data).
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import askyesno
root = Tk()
root.geometry( '200x100' )
input_text = StringVar()
entry1 = ttk.Entry(root, textvariable = input_text, justify = CENTER)
entry1.focus_force()
entry1.pack(side = TOP, ipadx = 30 , ipady = 6 )
save = ttk.Button(root, text = 'Save' , command = lambda : askyesno(
'Confirm' , 'Do you want to save?' ))
save.pack(side = TOP, pady = 10 )
root.mainloop()
|
Output:


In above output, as soon as you run the code a Tkinter window will appear and Entry widget is already focussed that means we don’t have to give focus to the Entry area.
When we press Button a confirmation message will appear, saying whether you want to save the text or not (it will not save the text, it is only used to show the functioning of Button).
Code #2: Adding Style to the entered text in Entry widget.
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import askyesno
root = Tk()
root.geometry( '200x100' )
input_text = StringVar()
style = ttk.Style()
style.configure( 'TEntry' , foreground = 'green' )
entry1 = ttk.Entry(root, textvariable = input_text, justify = CENTER,
font = ( 'courier' , 15 , 'bold' ))
entry1.focus_force()
entry1.pack(side = TOP, ipadx = 30 , ipady = 10 )
save = ttk.Button(root, text = 'Save' , command = lambda : askyesno(
'Confirm' , 'Do you want to save?' ))
save.pack(side = TOP, pady = 10 )
root.mainloop()
|
Output:

In the above output, you may notice that the color of the font is changed, font family is changed, size of the text is bigger than normal as well as text is written in bold. This is because we are adding style to the input text.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Nov, 2021
Like Article
Save Article