"1.0" mean Line 1 , first char. Similarly END is the string end including the line break. To remove line break we have to modify like this.
t1.get("1.0",'end-1c')
Adding data to a text box my_str1 is the string variable to insert.
t2.insert(tk.END, my_str1)
Above two lines are most 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
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.
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. )