Exploring Tkinter's ScrolledText Widget (Python)

vegaseat 0 Tallied Votes 12K Views Share

The Tkinter GUI toolkit comes with a scrolled text widget that is called ScrolledText. To get access to it you have to import it separately. It allows you to display large amounts of text in a small space.

''' tk_scrolledtext101.py
explore Tkinter's ScrolledText widget
inside the edit_space use
ctrl+c to copy, ctrl+x to cut selected text,
ctrl+v to paste, and ctrl+a to select all

uses the same methods as the Text() widget
'''

try:
    # for Python2
    import Tkinter as tk
    import ScrolledText as tkst
except ImportError:
    # for Python3
    import tkinter as tk
    import tkinter.scrolledtext as tkst


root = tk.Tk()
root.title("ScrolledText")
frame = tk.Frame(root, bg='brown')
frame.pack(fill='both', expand='yes')

edit_space = tkst.ScrolledText(
    master = frame,
    wrap   = 'word',  # wrap text at full words only
    width  = 25,      # characters
    height = 10,      # text lines
    bg='beige'        # background color of edit area
)
# the padx/pady space will form a frame
edit_space.pack(fill='both', expand=True, padx=8, pady=8)

mytext = '''\
Man who drive like hell, bound to get there.

Man who run in front of car, get tired.

Man who run behind car, get exhausted.

The Internet: where men are men, women are men, and children are FBI agents.
'''

edit_space.insert('insert', mytext)

root.mainloop()

# optiona info
#help(tkst.ScrolledText)
BustACode 15 Light Poster

Great stuff. I did't know that that capability was availble. Thanks.

toyota_1 0 Newbie Poster

I wanted to add another textbox to bottom. That textbox act as input. Just single line only but no scrolledtext.
in old python2:
entry = Entry(root, width = 20)
entry.pack( side = BOTTOM )
How can I used scrolledtedtext without scrolling?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.