Tkinter vertical scrollbar doesn't work functionally, it only shows small bar

96 Views Asked by At

I need to add a vertical scroll bar for the "treeview_1" which is inside a grid.

Basically, I created a grid. Inside a grid

so, my solution is at this part

# scrollbar
    vsb = ttk.Scrollbar(frame, orient="vertical", command=treeview_1.yview)
    vsb.grid(row=1, column=0, sticky="e", columnspan=2)
    treeview_1.configure(yscrollcommand=vsb.set)

this is the result, enter image description here

This is not a functional scrollbar ..., even though I fill in data.

3

There are 3 best solutions below

0
On

Since there is no person wants to answer this question, I have to do some research for myself.

Luckily, there is an answer:

vscrollbar = tk.Scrollbar(frame, orient="vertical", command=treeview_1.yview)
vscrollbar.grid(row=1, column=1, sticky="NSE")
treeview_1.configure(yscrollcommand=vscrollbar.set)

The core issue here is the sticky option, it can be north-south-east which is exactly "NSE".

Done.

0
On

Please find my example. It might help you.

import tkinter as tk
from tkinter import ttk

# Create the main window
root = tk.Tk()
root.title("Vertical Scrollbar Example")

# Create a grid
frame = ttk.Frame(root, padding="10")
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))

# Create a Treeview widget
treeview_1 = ttk.Treeview(frame, columns=("Column 1", "Column 2"), show="headings")
treeview_1.grid(row=0, column=0, sticky="nsew")

# Add some sample data to the Treeview
treeview_1.heading("Column 1", text="Column 1")
treeview_1.heading("Column 2", text="Column 2")

for i in range(1, 21):
    treeview_1.insert("", "end", values=(f"Item {i}", f"Description {i}"))

# Create a vertical scrollbar
vsb = ttk.Scrollbar(frame, orient="vertical", command=treeview_1.yview)
vsb.grid(row=0, column=1, sticky="ns")
treeview_1.configure(yscrollcommand=vsb.set)

# Allow the Treeview and the Scrollbar to expand with the window
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

# Run the application
root.mainloop()
2
On

The code you provided is correct. If you're not seeing scroll bars in your development environment while running this code, you may need to check your environment settings or preferences to enable or show scroll bars. The code you provided doesn't influence the appearance of scroll bars. I have a code to create a scroll bar in Tkinter, I hope it will help you.

import tkinter as tk

def create_text_with_content(root):
    text = tk.Text(root, wrap=tk.NONE, yscrollcommand=scrollbar.set)
    text.pack(fill=tk.BOTH, expand=True)

    for i in range(20):
        text.insert(tk.END, f"Line {i}\n")

    return text

def on_scroll(*args):
    text.yview_moveto(args[0])

root = tk.Tk()
root.geometry("300x300")

scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL)
scrollbar.pack(fill=tk.Y, side=tk.RIGHT)

text_widget = create_text_with_content(root)
text_widget.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=on_scroll)

root.mainloop()