Change language

Tkinter | Adding style to entered text using the ttk.Entry widget

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.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically