How to attach a Scrollbar to a Text widget?

Question:

I am trying to attach a scrollbar to my Text field and have been unable to do so. Here is the segment of code:

self.scroller = Scrollbar(self.root)
self.scroller.place(x=706, y=121)
self.outputArea = Text(self.root, height=26, width=100) 
self.outputArea.place(x=0, y=120)
self.scroller.config(command=self.outputArea.yview)
self.outputArea.config(state=DISABLED, yscrollcommand = self.scroller.set)

This code is placing a very small scrollbar right next to my text field (and by very small, I mean that you can see the up and down arrows but nothing in between). I can scroll with it when my text field fills up, but is there a way to at the very least set the height of the scrollbar so that it appears to be the same height as the text field?

Asked By: jwebster

||

Answers:

Tkinter has three geometry managers: pack, grid, and place.
Pack and grid are usually recommended over place.

You can use the grid manager’s row and column options
to position the Scrollbar next to the Text widget.

Set the Scrollbar widget’s command option to the Text’s yview method.

scrollb = tkinter.Scrollbar(..., command=txt.yview)

Set the Text widget’s yscrollcommand option to the Scrollbar’s set method.

txt['yscrollcommand'] = scrollb.set

Here’s a working example that makes use of ttk:

import tkinter
import tkinter.ttk as ttk

class TextScrollCombo(ttk.Frame):

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

    # ensure a consistent GUI size
        self.grid_propagate(False)
    # implement stretchability
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

    # create a Text widget
        self.txt = tkinter.Text(self)
        self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)

    # create a Scrollbar and associate it with txt
        scrollb = ttk.Scrollbar(self, command=self.txt.yview)
        scrollb.grid(row=0, column=1, sticky='nsew')
        self.txt['yscrollcommand'] = scrollb.set

main_window = tkinter.Tk()

combo = TextScrollCombo(main_window)
combo.pack(fill="both", expand=True)
combo.config(width=600, height=600)

combo.txt.config(font=("consolas", 12), undo=True, wrap='word')
combo.txt.config(borderwidth=3, relief="sunken")

style = ttk.Style()
style.theme_use('clam')

main_window.mainloop()

The part that will address your Scrollbar being small is sticky='nsew',
which you can read about → here.

Something that will be helpful for you to learn right now is that different Tkinter widgets can use different geometry managers within the same program as long as they do not share the same parent.


The tkinter.scrolledtext module contains a class called ScrolledText which is a compound widget (Text & Scrollbar).

import tkinter
import tkinter.scrolledtext as scrolledtext

main_window = tkinter.Tk()

txt = scrolledtext.ScrolledText(main_window, undo=True)
txt['font'] = ('consolas', '12')
txt.pack(expand=True, fill='both')

main_window.mainloop()

The way this is implemented is worth taking a look at.

Answered By: Honest Abe

If you’re working with an INPUT box, then a handy way using the scrolledtext function. It took me 4+ hours to find it. Don’t ya love tkinter?

Two things to note…
The additional import required
import tkinter.scrolledtext as tkscrolled
and you set default value using insert and read the value using get (more terrible naming)

This bit of code was central to making my 20 character wide by 10 lines text input box to work.

import tkinter.scrolledtext as tkscrolled
import tkinter as tk

default_text = '1234'
width, height = 20,10
TKScrollTXT = tkscrolled.ScrolledText(10, width=width, height=height, wrap='word')

# set default text if desired
TKScrollTXT.insert(1.0, default_text)
TKScrollTXT.pack(side=tk.LEFT)

The scroll bars show up once reaching the height as defined in the call. They are grayed out and blend into the background well. It works great….. once you figure out the correct calls to make.

I hope this is relevant to your question!

Answered By: Mike from PSG

You can use tkinter frame for it, this is very simple way =>

import tkinter as tk
from tkinter import *
root = tk.Tk()

f = tk.Frame(root)
f.place(x=10, y=20)

scrollbar = Scrollbar(f)
t = tk.Text(f, height=10, width=10, yscrollcommand=scrollbar.set)
scrollbar.config(command=t.yview)
scrollbar.pack(side=RIGHT, fill=Y)
t.pack(side="left")

root.mainloop()
Answered By: user9135988

I believe this would work. This is a piece of my code that I used on mine. There are some other options that you can remove. I have them because my theme is dark.

from tkinter import *

window = Tk()
window.title("test for scrollbar")
window.config(pady=50, padx=50, bg="white")


text_box = Text(width=49, height=5, bg="white", highlightthickness=1, foreground="black",
                    insertbackground="black", wrap="word")
    tex_scroll = Scrollbar(orient=VERTICAL,)
    
    tex_scroll.config(command=text_box.yview, )
    text_box["yscrollcommand"] = tex_scroll.set
    
    text_box.grid(column=1, row=4, columnspan=2, sticky="w")
    tex_scroll.grid(column=2, row=4, sticky="nse")

window.mainloop()
Answered By: zorg
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.