I have been doing a lot of Tkinter lately and I was wondering of there is a way i fit a scroll bar into a text wdiget using the .grid() method.
right now the code i have writen is:

from Tkinter import *
root = Tk()

text = Text(root)
scrl = Scrollbar(root, command=text.yview)
scrl.grid(row=0, column=1)


i know that you can use pack(side=RIGHT, fill=Y), but i don't knwo how to use pack, and i can;y use both.

can anyone tell me how to make it fit into the text using .grid()?

Recommended Answers

All 2 Replies

Here is an example for a listbox, the textbox should be similar ...

# Tk_ListBoxScroll1.py
# simple example of Tkinter listbox with scrollbar

    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
# use width x height + x_offset + y_offset (no spaces!)
root.title('listbox with scrollbar')

# create the listbox (height/width in char)
listbox = tk.Listbox(root, width=20, height=6)
listbox.grid(row=0, column=0)

# create a vertical scrollbar to the right of the listbox
yscroll = tk.Scrollbar(command=listbox.yview, orient=tk.VERTICAL)
yscroll.grid(row=0, column=1, sticky='ns')

# now load the listbox with data
friend_list = [
'Stew', 'Tom', 'Jen', 'Adam', 'Ethel', 'Barb', 'Tiny',
'Tim', 'Pete', 'Sue', 'Egon', 'Swen', 'Albert']
for item in friend_list:
    # insert each new item to the end of the listbox
    listbox.insert('end', item)

# optionally scroll to the bottom of the listbox
lines = len(friend_list)
listbox.yview_scroll(lines, 'units')


although all i really needed was:
.grid(row=0, column=1, sticky='ns')

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.