-1

I followed some tutorial on attaching a scrollbar to a textbox. However, in the tutorial, the scrollbar is really a "bar". When I tried myself, I can only press the arrows to move up or down, the middle part is not movable. May I know what I did wrong?

import tkinter as tk

root = tk.Tk()
scroll = tk.Scrollbar(root)
scroll.grid(row = 0, column = 1)
message = tk.Text(root, yscrollcommand = scroll.set, height = 25, width = 60)
message.grid(row = 0, column = 0)
for i in range(50):
    message.insert(tk.END, f'This is line {i}\n')
scroll.config(command = message.yview)

root.mainloop()

Tk window generated by the code

Mayan
  • 492
  • 4
  • 11
  • Perhaps this helps: https://stackoverflow.com/questions/22047206/stretch-scrollbar-to-canvas-size-in-tkinter – sshashank124 Dec 30 '19 at 07:39
  • ***"in the tutorial, the scrollbar is really a "bar""***: Change to `scroll.grid(.., sticky='ns')` – stovfl Dec 30 '19 at 08:54
  • Also there is a `ScrolledText` available which is basically a `Text` widget with a built-in scrollbar. `from tkinter.scrolledtext import ScrolledText` – Henry Yik Dec 30 '19 at 09:17

1 Answers1

0

You just have to add sticky='nsew' to Scrollbar widget.

sticky='nsew' will make the Scrollbar widget to expand to fill up the entire cell (at grid position row=0 & column=1) at every side (n-north, s-south, e-east, w-west)

Here is the code:

import tkinter as tk

root = tk.Tk()
scroll = tk.Scrollbar(root)

# add sticky option to the Scrollbar widget
scroll.grid(row = 0, column = 1, sticky='nsew')

message = tk.Text(root, yscrollcommand = scroll.set, height = 25, width = 60)
message.grid(row = 0, column = 0)
for i in range(50):
    message.insert(tk.END, f'This is line {i}\n')
scroll.config(command = message.yview)

root.mainloop()
Somraj Chowdhury
  • 983
  • 1
  • 6
  • 14