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 tkroot = 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()
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 tkroot = 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()