Python tkinter Text box
Python text box widget is same as a label or message. But Text widget has several options and can display multiple lines of text in different colors and fonts.
tkinter Text box widget
It is possible to insert text into a Text widget, modify it or delete it. We can also display images in the Text widget. One can create a Text widget by creating an object to Text class as:
t = Text(root, width=20, height=10, font=('Verdana', 14, 'bold'), fg='blue', bg='yellow', wrap=WORD)
Here, 't' represents the object of Text class.'root' represents an object of root window or frame.'width' represents the width of the Text widget in characters.'height' represents the height of the widget in lines.The option 'wrap' specifies where to cut the line.wrap=CHAR represents that any line that is too long will be broken at any character.wrap=WORD will break the line in the widget after the last word that fits in the line. wrap=NONE will not wrap the lines. In this case, it is better to provide a horizontal scroll bar to view the lines properly in the Text widget.
Once the Text widget is created, we can insert any text using the insert() method as:
t.insert(END, 'Text widget\nThis text is inserted into the Text widget.\n This is second line\n and this is third line\n')
Here, the first argument END represents that the text is added at the end of the previous text.We can also use CURRENT to represent that the text is added at the current cursor position.The second argument is the text that is added to the Text widget. It is possible to display an image like a photo using the image_create() method as:
img = PhotoImage(file='moon.gif')# store moon.gif into img object
t.image_create(END, image=self.img)#append img to Text widget at the end
It is possible to mark some part of the text as a tag and provide different colors and font for that text. For this purpose, first we should specify the tag using the tag_add() method as:
t.tag_add('start', '1.0', '1.11')
Here, the tag name is 'start'.It contains characters (or text) from 1st row 0th character till 1st row 11th character.Now, we can apply colors and font to this tag text using the config() method as:
t.tag_config('start', background='red', foreground='white', font=('Lucida console', 20, 'bold italic'))
font=('Lucida console', 20, 'bold italic'))
Here, we are applying 'red' background and 'white' foreground and 'Lucida console' font to the text that is already named as 'start' tag.
In many cases, it is useful to add scroll bars to the Text widget. A scroll bar is a bar that is useful to scroll the text either horizontally or vertically.For example, we can create a vertical scroll bar by creating an object to Scrollbar class as:
s = Scrollbar(root, orient=VERTICAL, command= t.yview)
Here, 'orient' indicates whether it is a vertical scroll bar or horizontal scroll bar.The 'command' option specifies to which widget this scroll bar should be connected.'t.yview' represents that the scroll bar is connected to 't', i.e.Text widget and 'yview' is for vertical scrolling.
We should set the 'yscrollcommand' option of the Text widget 't' to 's.set' method. In this way, the scroll bar 's' is connected to Text widget 't'.
Text box example
A Python program to create a Text widget with a vertical scroll bar attached to it. Also, highlight the first line of the text and display an image in the Text widget
from tkinter import * class MyText: def __init__(self, root): self.t = Text(root, width=20, height=10, font=('Verdana', 14, 'bold'), fg='blue', bg='yellow', wrap=WORD) self.t.insert(END, 'Text widget\nThis text is inserted into the Text widget.\n This is second line\n and this is third line\n') #attach Text to the root self.t.pack(side=LEFT) #show image in the Text widget self.img = PhotoImage(file='moon.gif') self.t.image_create(END, image=self.img) #create a tag with the name 'start' self.t.tag_add('start', '1.0', '1.11') #apply colors to the tag self.t.tag_config('start', background='red', foreground='white',font=('Lucida console', 20, 'bold italic')) #create a Scrollbar widget to move the text vertically self.s = Scrollbar(root, orient=VERTICAL, command= self.t.yview) #attach the scroll bar to the Text widget self.t.configure(yscrollcommand=self.s.set) #attach the scroll bar to the root window self.s.pack(side=RIGHT, fill=Y) root = Tk() mt = MyText(root) root.mainloop()