Tkinter horizontal Scrollbar not working as expected with Treeview

Viewed 63

I have created a Treeview widget to be filled later, with file names and paths. The horizontal and vertical scrollbars are created.

But when I load the Treeview with a line that exceeds its width, the cursor in the horizontal Scrollbar does not show up (can't scroll to the right). When several lines are loaded the vertical Scrollbar works as expected.

Here is a sample code to test what I have done:

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
    """Root window of tkinter app"""
    def __init__(self):
        super().__init__()

        self.title('Test')
        Output(self)


class Output(ttk.Frame):
    """Tab frame displaying analysis output"""
    def __init__(self, parent):
        super().__init__(parent)

        self.grid(row=0, column=0, columnspan=2, rowspan=6, padx=5, pady=5, sticky=tk.NSEW)
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=3)

        self.results = None

        self.files_panel = FilesPanel(self)

        self.previous = CommandPanel(self)
        self.previous.load_btn['command'] = self.load_treeview

    def load_treeview(self):
        dummy_path = ['dummy_path', 'Long/path/to/file/somewhere/requires/scrollbar/to/be/read']
        self.files_panel.tv.insert('', tk.END, values=dummy_path)


class FilesPanel(ttk.LabelFrame):
    def __init__(self, parent):
        super().__init__(parent)

        self.config(text='Analysed files')
        self.grid(row=0, column=0, padx=5, pady=5, sticky=tk.NSEW)

        columns = ('file_name', 'path')
        self.tv = ttk.Treeview(self, columns=columns, show='headings')
        self.tv.heading('file_name', text='File')
        self.tv.column('file_name', width=100)
        self.tv.heading('path', text='Path')
        self.tv.column('path', width=200)
        self.tv.grid(row=0, column=0, padx=5, pady=5, sticky=tk.NSEW)
        self.add_scrollbars(self)

    def add_scrollbars(self, container):
        self.sb_x = ttk.Scrollbar(container, orient=tk.HORIZONTAL, command=self.tv.xview)
        self.sb_y = ttk.Scrollbar(container, orient=tk.VERTICAL, command=self.tv.yview)
        self.tv.configure(xscrollcommand=self.sb_x.set, yscrollcommand=self.sb_y.set)
        self.sb_x.grid(row=1, column=0, sticky=tk.EW)
        self.sb_y.grid(row=0, column=1, sticky=tk.NS)


class CommandPanel(ttk.LabelFrame):
    def __init__(self, parent):
        super().__init__(parent)

        self.output = parent

        self.config(text='Previous results')
        self.grid(row=1, column=0, padx=5, pady=5, sticky=tk.EW)

        self.load_btn = ttk.Button(self, text='Load')
        self.load_btn.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)


if __name__ == '__main__':
    app = App()
    app.mainloop()

NB:

0 Answers
Related