Login Widgets are used to retrieve an entry from a user. It can be created like this:
entry = ttk.Entry (master, option = value, ...)
Code # 1: Creating a widget Entry and getting user input (getting only String data).
# tkinter imports from tkinter import * from tkinter import ttk from tkinter.messagebox import askyesno # creating root root = Tk () # specifying geometry root.geometry ( ’200x100’ ) # This is used to get input from the user # and show it in the Entry Widget. # Whatever data we receive from the keyboard # will be treated as a string. input_text = StringVar () entry1 = ttk.Entry (root, textvariable = input_text, justify = CENTER) # focus_force used for focus # as soon as the application starts 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 the above output, once you run the code, the button, a confirmation message appears asking if you want to save the text or no (the text will not be saved, it is only used to display the button’s functionality).
Code # 2: add style to the entered text in the input widget.
# tkinter import from tkinter import * from tkinter import ttk from tkinter.messagebox import askyesno # create root root = Tk () root.geometry ( ’ 200x100’ ) input_text = StringVar () # This class is used to add style # to any available widget 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 font color has changed, the font family has changed, the text is larger than normal, and the text is in bold. This is because we are adding styling to the text we enter.