Formatting tkinter Entry

We can format the inputs by using different events and one common requirement is formatting to local system. We are using locale library.
import locale
Here is the code to format the input as entered by user and on lost focus or once the focus is moved out of the input box the data is formatted. The required format can be changed based by using setlocal().
locale.setlocale(locale.LC_ALL, 'en_US.utf8')# USA

Function show_format()

This function is triggered once the input box lost the focus and the code to format the input is kept here. The triggering event can be changed .
def show_format(*args):
    e1_str.set(locale.format_string("%.2f", float(e1.get()), grouping=True))
Here e1_str is the string variable connected to entry option. So we used set() option of string variable to assign value to entry box.

Triggering the event to execute the function

Here we used on focus out event to trigger the function, however this event can be changed to any other required event.
e1.bind("<FocusOut>",show_format) # when tab is pressed
#e1.bind("<KeyRelease>",show_format) # when key is released

Output

Data entered is 12345678.456
#locale.setlocale(locale.LC_ALL, 'de_DE.utf8')# German
German Format of number
locale.setlocale(locale.LC_ALL, 'en_US.utf8')# USA
English Format of number

Without using locale library

Inside the function show_format() we can use this line to get string formatting.
e1_str.set("{:,}".format(int(e1.get())))
Full code is here
import tkinter as tk
import locale
#locale.setlocale(locale.LC_ALL, 'de_DE.utf8')# German
locale.setlocale(locale.LC_ALL, 'en_US.utf8')# USA
my_w = tk.Tk()
my_w.geometry("250x100") 
font1=('Times',18,'bold')	
sv = tk.StringVar() # String variable 
def show_format(*args):
    #e1_str.set("{:,}".format(int(e1.get())))
    e1_str.set(locale.format_string("%.2f", float(e1.get()), grouping=True))
e1_str=tk.StringVar() # string variable   
e1 = tk.Entry(my_w,font=font1,width=13,textvariable=e1_str)
e1.grid(row=1,column=1,padx=18,pady=5)
b1=tk.Button(my_w,text='Testing')
b1.grid(row=1,column=2)
e1.bind("<FocusOut>",show_format) # when tab is pressed
my_w.mainloop()  # Keep the window open
We can use on key release event and then format every 4th position. Check how the cursor position is collected and repositioned.
Format with thousand place
from tkinter import *
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x100") 
font1=('Times',18,'bold')	
sv = tk.StringVar() # String varible 
def show_format(*args):
    position = e1.index(INSERT) # getting cursor position
    if((position % 4)==0):
        e1.icursor(position+1) # shift cursor one position
    x=e1_str.get().replace(',','')
    e1_str.set("{:,}".format(float(x)))
  
e1_str=tk.StringVar() # string variable   
e1 = tk.Entry(my_w,font=font1,width=20,textvariable=e1_str)
e1.grid(row=1,column=1,padx=18,pady=5)
#e1.bind("<FocusOut>",show_format) # when tab is pressed
e1.bind("<KeyRelease>",show_format) # when key is released
my_w.mainloop()  # Keep the window open
Tkinter Entry Tkinter Text How to Validate user entered data
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer