Wednesday, January 30, 2019

tkinter- example 7 (Text widget)

The Text widget provides formatted text display. It allows you to display and edit text with various styles and attributes. The text widget is used to display text documents, containing either plain text or formatted text (using different fonts, embedded images, and other embellishments). Though one of the main purposes is to provide simple multi-line areas, as they are often used in forms, text widgets can also be used as simple text editors or even web browsers. Furthermore, text widgets can be used to display links, images, and HTML, even using CSS styles.

The Text widget is a bigger, more powerful version of the Entry widget. Here is the code to
create a text widget:

textbox = Text(font=('Verdana', 16), height=5, width=50)

The widget will be 50 characters wide and 5 rows tall. We can still type past the fifth row but the
widget will just display only five rows at a time. We can use the arrow keys to scroll. The following program creates the widget as shown in the output:

import tkinter as tk
from tkinter import *

root = tk.Tk()

textbox = Text(font=('Verdana', 16), height=5, width=50)

textbox.pack()
mainloop()

The above program creates this text widget:



We can type on this widget but as per the limitations set by program only 5 lines at a time will be visible. To make this text widget better, we can associate a scrollbar with the text box making it a ScrolledText widget. To use the ScrolledText widget, you will need the following import:

from tkinter.scrolledtext import ScrolledText

The following program creates the ScrolledText widget :

import tkinter as tk
from tkinter import *
from tkinter.scrolledtext import ScrolledText

root = tk.Tk()
textbox = ScrolledText(font=('Verdana', 16), height=5, width=50)
textbox.pack()
mainloop()

The above program creates this text widget:



As you can see we have entered in more than 5 lines and thus a scrollbar was associated with the text box. We can always scroll down to see the later entries as shown below:



We can also add a scrollbar using tkinter's Scrollbar() method.  We call it with the root object as the only parameter. The following program creates the scrollbar using the Scrollbar() method:


import tkinter as tk
from tkinter import *

root = tk.Tk()
S = Scrollbar(root)

textbox = Text(font=('Verdana', 16), height=5, width=50)
S.pack(side=RIGHT, fill=Y)
textbox.pack()
S.config(command=textbox.yview)
textbox.config(yscrollcommand=S.set)

mainloop()

The above program creates this text widget:



We can use some commands with the text box widget, here are some of them:

  1. textbox.get(1.0,END) - returns the contents of the text box
  2. textbox.delete(1.0,END) - deletes everything in the text box
  3. textbox.insert(END,'sometext') - inserts text at the end of the text box
The following program uses these commands:



import tkinter as tk
from tkinter import *

root = tk.Tk()
S = Scrollbar(root)

textbox = Text(font=('Verdana', 16), height=5, width=50)
S.pack(side=RIGHT, fill=Y)
textbox.pack()
S.config(command=textbox.yview)
textbox.config(yscrollcommand=S.set)
quote = "The text widget stores and displays lines of text.\nThe text body can consist of characters, marks, and embedded windows or images. \nDifferent regions can be displayed in different styles, and you can also attach event bindings to regions.\n By default, you can edit the text widget’s contents using the standard keyboard and mouse bindings."

textbox.insert(END, quote)
print(textbox.get(1.0,END))
textbox.delete(1.0,END)
root.mainloop()

The above program creates this text widget:





In the above program using insert () method we have inserted text in to the Text widget. Then using the get() method we printed the inserted text in the command window. Lastly we have cleared the text box using the delete() method.

We can also insert image in the text box by using the image as a PhotoImage object. The following program explains how to do this:


import tkinter as tk
from tkinter import *

root = Tk()

text1 = Text(root, height=20, width=30)
photo=PhotoImage(file='./covri.gif')
text1.insert(END,'\n')
text1.image_create(END, image=photo)

text1.pack(side=LEFT)

text2 = Text(root, height=20, width=60)
scroll = Scrollbar(root, command=text2.yview)
text2.configure(yscrollcommand=scroll.set)
text2.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
text2.tag_configure('big', font=('Verdana', 20, 'bold'))
text2.tag_configure('color', foreground='#476042',
font=('Tempus Sans ITC', 12, 'bold'))

text2.insert(END,'\nCOVRI\n', 'big')
quote = "\nCOVRI Solutions is a team of young professionals providing IT solutions for enterprises.\n\nWe are based in Hyderabad, the prime location for IT in India."
text2.insert(END, quote, 'color')

text2.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)

root.mainloop()

The above program creates this text widget with an image:



 Here I am ending today's discussion about the text box widget. Until we meet next keep practicing and learning Python as Python is easy to learn!
















Share:

0 comments:

Post a Comment