Reading data of text box starting from first char till end
t1.get("1.0",END)
"1.0" mean Line 1 , first char. Similarly END is the string end including the line break. To remove line break at the end we have to modify like this.
t1.get("1.0",'end-1c')
For a multi-line text above code will remove line break at the last line only. To calculate the total number of chars the user has entered inside the text widget, we have to count the number of line breaks ( excluding the last one ).
len() : Return the number of elements present in an iterable count() : Counts the number of matching string present.
my_str=t1.get("1.0",'end-1c') # excluding the last line break char.
breaks=my_str.count('\n') # Number of line breaks ( except last one )
char_numbers=len(my_str)-breaks # total chars excluding line breaks
Adding data to a text box my_str1 is the string variable to insert.
t2.insert(tk.END, my_str1)
Above codes are important to develop applications.
Note that the widely used option textvariable is not available with Text widget
There are many options we can add to text box, the list is available below.
Now let us add the click event of the button linked to text box. We will enter data in our text box , on click of the button the entered data will be displayed at another Label.
import tkinter as tk
from tkinter import BOTH, END, LEFT
my_w = tk.Tk()
my_w.geometry("500x300")
def my_upd():
my_str.set(t1.get("1.0",END)) # read the text box data and update the label
my_str = tk.StringVar()
l1 = tk.Label(my_w, text='Your Name', width=10 ) # added one Label
l1.grid(row=1,column=1)
t1 = tk.Text(my_w, height=1, width=20,bg='yellow')# added one text box
#t1=Entry(my_w, width=10)
t1.grid(row=1,column=2)
b1 = tk.Button(my_w, text='Update', width=10,bg='red',command=lambda: my_upd()) # button added
b1.grid(row=1,column=3)
l2 = tk.Label(my_w, textvariable=my_str, width=20 ) # added one Label
l2.grid(row=1,column=4)
my_str.set(" I will update")
my_w.mainloop()
Let us update one text box by using data from another text box.
import tkinter as tk
from tkinter import BOTH, END, LEFT
my_w = tk.Tk()
my_w.geometry("500x400")
def my_upd():
my_str1=t1.get("1.0",END) # read from one text box t1
t2.insert(tk.END, my_str1) # Add to another text box t2
my_str = tk.StringVar()
l1 = tk.Label(my_w, text='Your Name', width=10 ) # added one Label
l1.grid(row=1,column=1)
t1 = tk.Text(my_w, height=1, width=20,bg='yellow')# added one text box
#t1=Entry(my_w, width=10)
t1.grid(row=1,column=2)
b1 = tk.Button(my_w, text='Update', width=10,bg='red',command=lambda: my_upd()) # button added
b1.grid(row=1,column=3)
t2 = tk.Text(my_w, height=1, width=15, bg='#00f000' ) # added one textbox to read
t2.grid(row=1,column=4)
my_w.mainloop()
Delete ( or empty ) text box
We can remove data from first text box after updating the second text box. In above code you can two more lines inside the function.
def my_upd():
my_str1=t1.get("1.0",END) # read from one text box t1
t2.insert(tk.END, my_str1) # Add to another text box t2
t1.delete('1.0',END) # Delete from position 0 till end
t1.update() # update the delete
Remove all inputs from all text widgets
Irrespective of number of Text boxes we used in our application, we can remove all user entered data by using winfo_children() which returns all the widget classes of the Tkinter window.
Here is the code
for widget in my_w.winfo_children():
if isinstance(widget, tk.Text): # If this is an Entry widget
widget.delete(0,'end') # Delete all entries
l2.delete(1.4,1.7) # from 4th till 7th position of line one
l2.delete(1.0,END) # from starting till end
get()
print(l2.get(1.4,1.7)) # print from 4th till 7th position of line one
print(l2.get(1.0,END)) # Print from starting till end
tag_add(), tag_config() & tag_remove()
import tkinter as tk
from tkinter import BOTH, END, LEFT
my_w = tk.Tk()
my_w.geometry("400x300")
l1 = tk.Label(my_w,text='Your Name', width=10) #added one Label
l1.grid(row=1,column=1)
t1 = tk.Text(my_w,width=35,height=4) #text box
t1.grid(row=1,column=2,columnspan=2,pady=30)
font1=('Times',16,'underline')
t1.insert(tk.END, "Welcome to plus2net")
t1.tag_add("my_hg", "1.2", "1.13") # tag name my_hg is created
t1.tag_config("my_hg",background="yellow",foreground="red",font=font1)
t1.tag_remove('my_hg','1.5','1.10') # tag is removed from part
my_w.mainloop()
Using buttons to trigger events using tags
We can destroy the tag by using tag_delete(). Similarly we can use tag_remove() and tag_config() by using button clicks.
def select_all(): # to select all text inside Text box
e1.tag_add("sel", "1.0","end") # all text selected
e1.tag_config("sel",background="green",foreground="red")
Tkinter text widget adding tags by usign tag_add(), tag_config(),tag_remove() and tag_delete()
Exercise on Text
Ask user to enter Name, marks ( in three subjects Physics, Chemistry, Math ) and attendance of a student. On Update , the data will be stored in a Dictionary and finally in a CSV file. ( Break it in parts, First display the data using print(), then create Dictionary and then save as CSV file. )