t1.bind('<KeyRelease>',my_upd)
Inside the function my_upd() we will read the string entered by the user and calculate the total number of chars present in the string.
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("500x300") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='Your Data', width=10,font=20 ) # added one Label
l1.grid(row=0,column=0,padx=3,pady=10,columnspan=2)
t1=tk.Text(my_w,height=6,width=40,bg='lightgreen',font=28)
t1.grid(row=1,column=0,padx=10,columnspan=2)
l2=tk.Label(my_w,text=0,font=22)
l2.grid(row=2,column=0,padx=5,pady=5)
def my_upd(value):
my_str=t1.get('1.0','end-1c') #The input string except the last line break
breaks=my_str.count('\n') # Number of line breaks ( except the last one )
char_numbers=len(my_str)-breaks # Number of chars user has entered
l2.config(text=str(char_numbers)) # display number of chars
if(char_numbers > 20):
t1.delete('end-2c') # remove last char of text widget
t1.bind('<KeyRelease>',my_upd) # Key release event to call function.
my_w.mainloop() # Keep the window open
prg1['value']=char_numbers
By using ttk.Style
we can manage the colour of the progress bar. Here we are using config() to update the style properties. import tkinter as tk
from tkinter import ttk
from tkinter import END
my_w = tk.Tk()
my_w.geometry("500x300") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='Your Data', width=10,font=20 ) # added one Label
l1.grid(row=0,column=0,padx=3,pady=10,columnspan=2)
t1 = tk.Text(my_w,height=6, width=40,bg='lightgreen',font=28) # text box
t1.grid(row=1,column=0,padx=10,columnspan=2)
l2=tk.Label(my_w,text=0,font=22)
l2.grid(row=2,column=0,padx=5,pady=5)
def my_upd(value):
my_str=t1.get("1.0",'end-1c')
breaks=my_str.count('\n')
char_numbers=len(my_str)-breaks
l2.config(text=str(char_numbers))
prg1['value'] =char_numbers # Update Progress bar value
if(char_numbers < 10):
prg1.config(style='green.Horizontal.TProgressbar')
elif(char_numbers>20):
t1.delete('end-2c') # remove last char from Text widgets
prg1.config(style='red.Horizontal.TProgressbar')
else:
prg1.config(style='yellow.Horizontal.TProgressbar')
t1.bind('<KeyRelease>', my_upd)
s = ttk.Style()
s.theme_use('alt')
s.configure("red.Horizontal.TProgressbar", background='red')
s.configure("yellow.Horizontal.TProgressbar", background='yellow')
s.configure("green.Horizontal.TProgressbar", background='green')
prg1=ttk.Progressbar(my_w,length=350,mode='determinate',maximum=20,value=0,
style='green.Horizontal.TProgressbar')
prg1.grid(row=2,column=1,padx=0,pady=5)
my_w.mainloop() # Keep the window open
import tkinter as tk
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("420x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
l1 = tk.Label(my_w, text="Your Data", width=10, font=20) # added one Label
l1.grid(row=0, column=0, padx=3, pady=10, columnspan=2)
t1 = tk.Text(my_w, height=3, width=15, bg="lightgreen", font=28) # text box
t1.grid(row=1, column=0, padx=10)
frame_side = Frame(my_w, width=380, height=150) # frame to hold counter and progress bar
frame_side.grid(row=1, column=3, columnspan=1)
l2 = tk.Label(frame_side, text=0, font=22) # show count inside frame
l2.grid(row=1, column=0, padx=5, pady=5)
def my_upd(value):
my_str = t1.get("1.0", 'end-1c')
breaks = my_str.count('\n')
char_numbers = len(my_str) - breaks
l2.config(text=str(char_numbers))
prg1['value'] = char_numbers # Update Progress bar value
if char_numbers < 10:
prg1.config(style='green.Horizontal.TProgressbar')
elif char_numbers > 20:
t1.delete('end-2c') # remove last char from Text widgets
prg1.config(style='red.Horizontal.TProgressbar')
else:
prg1.config(style='yellow.Horizontal.TProgressbar')
t1.bind('<KeyRelease>', my_upd)
s = ttk.Style()
s.theme_use('alt')
s.configure("red.Horizontal.TProgressbar", background='red')
s.configure("yellow.Horizontal.TProgressbar", background='yellow')
s.configure("green.Horizontal.TProgressbar", background='green')
prg1 = ttk.Progressbar(frame_side, length=150, mode='determinate',
maximum=20, value=0, style='green.Horizontal.TProgressbar')
prg1.grid(row=1, column=1, padx=0, pady=5) # Place the progressbar in frame
l3 = tk.Entry(my_w, font=14, width=15, bg='yellow') # widget in parent window
l3.grid(row=3, column=0, padx=5, pady=5)
def frame_show(*args):
frame_side.grid() # show the frame and its widgets
def frame_hide(*args):
frame_side.grid_remove() # hide the frame along with its widgets
t1.bind('<FocusIn>', frame_show) # Triggered when the widget gains focus
t1.bind('<FocusOut>', frame_hide) # Triggered when the widget loses focus
my_w.mainloop() # Keep the window open
Author
🎥 Join me live on YouTubePassionate 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.