Restricting number of chars in Text input


Tkinter Text Progress Bar

Part I ( without Progressbar )

Counting and restricting Number of chars entered in Text Widget
We will restrict the number of chars user can enter inside a Text widget.
The KeyRelease event of Text widget ( t1 ) is used to trigger the function to calculate the number of chars present.
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.
Details on Counting number of Chars Present inside Text Widget
If the number of chars is more than 20 then the last char is deleted so the user can't add more than 20 chars.

Full code is here.
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
Tkinter counting and managing the user entered chars in a Text Widget and restricting after a limit


Part II : Adding Progress bar to show the progress

Adding Progressbar to Text counter
Inside the update function first we will integrate the number showing the total number of chars entered by the user in the Text widget with the Progress bar.
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.

Our Progress bar will have three different levels, up to 10 chars the colour will be green. From 10 to 20 the colour will be yellow and after 20 it will be red.

By using if elif and else condition check we will add different styles to the progress bar based on the number of input chars.
Manging colour of Progress bar by using ttk-style
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
Tkinter Text Python Tkinter Entry How to Validate user entered data
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    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 FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer