0

I have realised that the scrollbar in Tkinter is by default hidden unless and until you have put enough texts,buttons,etc on your frame to make the screen scrollable. Is there any way to show the scrollbar even when there's nothing present inside the frame?

screenshot

screenshot

The photo shown in the first link has the text "Sample scrolling label" only once whereas in the next link the text "Sample scrolling label" is shown 150 times and the scrollbar is now visible

Here is my code

from tkinter import *


root = Tk()
root.geometry("1366x768")
container = Frame(root)
canvas = Canvas(container)
scrollbar = Scrollbar(container,cursor="dot",width=30, orient="vertical", 
command=canvas.yview,elementborderwidth=100)
scrollable_frame = Frame(canvas)

scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
    scrollregion=canvas.bbox("all",10,10)
)
)

canvas.create_window((0,0),width=200, window=scrollable_frame)

canvas.configure(yscrollcommand=scrollbar.set)
for i in range(150):
Label(scrollable_frame, text="Sample scrolling label").pack()//This text appears on the frame 150 
times

container.place(x=500,y=0,height=700,width=868)
canvas.pack(side=LEFT, fill=BOTH, expand=False)
scrollbar.pack(side="right", fill="y")

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61

1 Answers1

0

Is there any way to show the scrollbar even when there's nothing present inside the frame?

I assume you mean the presence of the thumb or slilder inside the scrollbar. The scrollbar as a whole will appear no matter what the contents.

The answer to your question is largely "no". The appearance of the thumb inside the scrollbar is controlled by the scrollbar. However, there might be some non-standard themes that make the thumb omnipresent. Hiding the thumb when there's nothing to scroll is pretty common though.

The one exception is the canvas. The scrollable region is completely under your control via the scrollregion attribute. Even in the case of the canvas, if the scrollable region is the same size (or smaller) than the window, the thumb won't appear.

The following example shows how to set the scrollable region to be twice the height of the canvas itself, and in this case the scrollbar thumb will show.

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=400, background="bisque")
scrollbar = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)

canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")

# set the scrollable area to 400x800
canvas.configure(scrollregion=(0, 0, 400, 800))

root.mainloop()

screenshot

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685