hrizontal scrol bar to a treeview+tkinter

Viewed 20

I want to add a horizontal scroll bar to the particular tree-view table since I have not used Tkinter before so please help!

import tkinter

def __create_tree_view(self):
        """ Creates a TreeView Layout with req Headers """
        style = it.Style()
        style.configure("Custom.Treeview.Heading",foreground="green", relief="flat")
        style.map("Custom.Treeview.Heading", relief=[('active','groove'),('pressed','sunken')])
        style.configure('Treeview', rowheight=20)
        #
        self.tree_view = ttk.Treeview(self.right_frame, selectmode ='browse',\
             style="Custom.Treeview")
        self.tree_view.grid(row=1, column=1, sticky=tk.NSEW)

        self.verticalscrollba = ttk.Scrollbar(self.right_frame, orient="vertical", \
            command=self.tree_view.view)
        self. verticalscrollbar.grid(row=1,column=1,sticky='nse')
        self.tree_view.configure(yscrollcommand = self.verscrlbar.set)
        # Defining number of columns
        self.tree_view["columns"] = ("1", "2", "3", "4")
        # Defining heading
        self.tree_view['show'] = 'headings'
        # Assigning the width and anchor to the respective columns
        self.tree_view.column("1", width = int(self.window_width/5),    anchor ='ca, stretch=False)
        self.tree_view.column("2", width = int(self.window_width/4)-30,    anchor ='ca, stretch=False)
        self.tree_view.column("3", width = int(self.window_width/6)-30, anchor ='ca, stretch=False)
        self.tree_view.column("4", width = int(self.window_width/3),    anchor ='ca, stretch=False)
        # Assigning the heading names to the respective columns
        self.tree_view.heading("1", text ="Brand/Model/Type")
        self.tree_view.heading("2", text ="Device_Id")
        self.tree_view.heading("3", text ="Device_Ip")
        self.tree_view.heading("4", text ="GUID")

where the brand is the brand of the device IP, is IP address and so here in this code how to add a horizontal scroll bar without overwriting the last line of the treeview table I tried to add the scrollbar but it's displaying on the last row of the table it should display below the last row of the table

1 Answers

Try this an example.

import tkinter as tk
from tkinter import ttk

class Chrisdb1(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.tvw_results = ttk.Treeview(self, style = 'mystyle.Treeview')
        self.insert = self.tvw_results.insert

        self.tvw_results.grid(row = 0, column = 0, sticky='nsew')
        self.tvw_results.column("#0", minwidth = 1150)
        self.tvw_results.heading('#0', text = 'Found results', anchor = tk.W)

        # Scrollbars + attach scrollbars to TreeView
        sb_vertical = tk.Scrollbar(self, orient = "vertical", command = self.tvw_results.yview)
        sb_horizontal = tk.Scrollbar(self, orient = "horizontal", command = self.tvw_results.xview)
        self.tvw_results.configure(yscrollcommand = sb_vertical.set, xscrollcommand = sb_horizontal.set)
        sb_vertical.grid(row = 0, column = 1, sticky = "ns")
        sb_horizontal.grid(row = 1, column = 0, sticky = "ew")

        # Configure and position grid for TreeView
        self.grid_rowconfigure(0, weight = 1)
        self.grid_columnconfigure(0, weight = 1)

def main():
    '''test code for Chrisdb1 class'''
    root = tk.Tk()
    cnt_treeview = Chrisdb1(root)
    for i in range(20):
        cnt_treeview.insert('', tk.END, text = f'Row {i}: Some very long texxxxxxxt.....'*6)
    cnt_treeview.pack(expand=True, fill=tk.BOTH)
    root.mainloop()

if __name__ == '__main__':
    main()
Related