GUI Programming With Python - Text Widget
GUI Programming With Python - Text Widget
TEXT WIDGETS
A text widget is used for multi-line text area. The Tkinter text
widget is very powerful and flexible and can be used for a
wide range of tasks. 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.
In most other tutorials and text books, it's hard to find a very
simple and basic example of a text widget. That's why we want
to start our chapter with a such an example:
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
Let's change our little example a tiny little bit. We add another text, the beginning of the famous monologue
from Hamlet:
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
quote = """HAMLET: To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
http://www.python-course.eu/tkinter_text_widget.php 1/4
10/7/2014 GUI Programming with Python: Text Widget
Devoutly to be wished."""
T.insert(END, quote)
mainloop()
If we start our little script, we get a very unsatisfying result. We can see in the window only the first line of
the monologue and this line is even broken into two lines. We can see only two lines in our window, because
we set the height to the value 2. Furthermpre, the width is set to 30, so Tkinter has to break the first line of the
monologue after 30 characters.
One solution to our problem consists in setting the height to the number of lines of our monologue and set
width wide enough to display the widest line completely.
But there is a better technique, which you are well acquainted with from your browser and other applications:
scrolling
SCROLLBARS
So let's add a scrollbar to our window. To this purpose, Tkinter provides the Scrollbar() method. We call it
with the root object as the only parameter.
root = Tk()
S = Scrollbar(root)
T = Text(root, height=4, width=50)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
quote = """HAMLET: To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished."""
T.insert(END, quote)
mainloop( )
The result is a lot better. We have now always 4 lines in view, but all lines can be viewed by using the
scrollbar on the right side of the window:
http://www.python-course.eu/tkinter_text_widget.php 2/4
10/7/2014 GUI Programming with Python: Text Widget
In our next example, we add an image to the text and bind a command to a text line:
root = Tk()
text1.pack(side=LEFT)
root.mainloop()
http://www.python-course.eu/tkinter_text_widget.php 3/4
10/7/2014 GUI Programming with Python: Text Widget
© 2011 - 2014 Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein
http://www.python-course.eu/tkinter_text_widget.php 4/4