Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Treeview scrollbar

My environment is Windows 7 using Python 64-bit 3.4. I am trying to attach a scrollbar to a treeview widget. I have tried several versions but have been unable to get it to work. What I have tried so far:

# treeview example
from tkinter import ttk
from tkinter import *

# Create instance
win = Tk()

# Add a title       
    win.title("Treeview Test")

# Add a treeview
    tree = ttk.Treeview(win,selectmode='browse')
    vsb = ttk.Scrollbar(orient="vertical",command=tree.yview)
    tree.configure(yscrollcommand=vsb.set)

    tree.place(x = 30, y = 95)
    tree["columns"] = ("1", "2")
    tree['show'] = 'headings'
    tree.column("1", width=100, anchor='c')
    tree.column("2", width=100, anchor='c')
    tree.heading("1", text="Account")
    tree.heading("2", text="Type")
    tree.insert("",'end',text="L1",values=("Big1","Best"))
    tree.insert("",'end',text="L2",values=("Big2","Best"))
    tree.insert("",'end',text="L3",values=("Big3","Best"))
    tree.insert("",'end',text="L4",values=("Big4","Best"))
    tree.insert("",'end',text="L5",values=("Big5","Best"))
    tree.insert("",'end',text="L6",values=("Big6","Best"))
    tree.insert("",'end',text="L7",values=("Big7","Best"))
    tree.insert("",'end',text="L8",values=("Big8","Best"))
    tree.insert("",'end',text="L9",values=("Big9","Best"))
    tree.insert("",'end',text="L10",values=("Big10","Best"))
    tree.insert("",'end',text="L11",values=("Big11","Best"))
    tree.insert("",'end',text="L12",values=("Big12","Best"))

# Set Window Form Size and disable resizing
    win.minsize(width=1200,height=600)
    win.resizable(width=0,height=0)  

# Event Loop
    win.mainloop()
    quit()
like image 600
Steve Baker Avatar asked Jan 26 '17 16:01

Steve Baker


People also ask

How do I add a scrollbar to Treeview in Python?

The Treeview widget allows the user to add a large number of lists along with the properties that can be customized instantly. If you want to attach a vertical scrollbar to the list of items in a Treeview widget, then you can define a constructor of Scrollbar and configure it by adding the command to it.

What is Treeview in tkinter?

Tkinter Treeview is a module in python which is used for constructing some attractive GUI (Graphical User Interface) for the python developers.


2 Answers

import tkinter as tk
from tkinter.ttk import Treeview, Scrollbar

top = tk.Tk()
top.geometry('1400x800')
top.configure(background="#f7f7f7")

# treeview initialization
treeview = Treeview(top)

# scrollbars
vsb = Scrollbar(top, orient="vertical", command=treeview.yview)
vsb.place(relx=0.978, rely=0.175, relheight=0.713, relwidth=0.020)

hsb = Scrollbar(top, orient="horizontal", command=treeview.xview)
hsb.place(relx=0.014, rely=0.875, relheight=0.020, relwidth=0.965)

treeview.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

May be this can help you.

like image 95
Rohit Patil Avatar answered Oct 23 '22 12:10

Rohit Patil


After creating your widgets, you need to place or lay out your widgets on the Tk window. The Layout Manager available in tkinter are grid, pack and place. Check them out and add them to your widgets.

like image 28
Sun Bear Avatar answered Oct 23 '22 11:10

Sun Bear