hello

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)
text.grid()
scrl = Scrollbar(root, command=text.yview)
text.config(yscrollcommand=scrl.set)
scrl.grid(row=0, column=1)

root.mainloop()

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()?
Thanks
The-IT

Dani AI

Generated

You can absolutely do this with grid. The key points are:

  • Put the Text and its scrollbar(s) in adjacent grid cells, make the Text fill its cell (sticky='nsew') and the scrollbars stretch along their axis ('ns' or 'ew').
  • Tell the geometry manager which row/column should expand by giving them weight; otherwise the Text will not resize with the window.
  • You cannot mix pack and grid in the same parent, but you can grid inside a Frame and pack that frame in the root if you need both.

If you also want a horizontal scrollbar, set wrap='none', place an x-oriented scrollbar below the Text, and wire xscrollcommand/xview accordingly. For automatic vertical scrolling without manual wiring on Python 3, ScrolledText is a convenient option:

from tkinter import Tk
from tkinter.scrolledtext import ScrolledText

root = Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

txt = ScrolledText(root, wrap='none')
txt.grid(row=0, column=0, sticky='nsew')

root.mainloop()

Relevant docs: the grid geometry options and weights are explained in the Tk man page at grid. Text widget scrolling behavior (including xview/yview and wrap) is documented at text. The Python wrapper for the ready-made scrolled text widget is at tkinter.scrolledtext.

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

try:
    # 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.geometry("240x180+130+180")
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')
listbox.configure(yscrollcommand=yscroll.set)

# 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')

root.mainloop()

TYVM
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.