Home > Mobile >  scroll bar disappears [Tkinter, treeview]
scroll bar disappears [Tkinter, treeview]

Time:10-31

I have a problem: when there are a large number of columns in the table, they go beyond the window field along with the scroll

Tell me what's wrong, please

My code:

from tkinter import *
from tkinter import ttk

class Main:
    def __init__(self):
        self.root = Tk()
        self.root.geometry('500x300')

    def run(self):
        self.interface()
        self.content()

    def interface(self):

        frame = Frame()
        frame.pack(fill = BOTH, expand=1)
                
        self.tree = ttk.Treeview(frame)
        self.tree.pack(fill = BOTH, expand=1, side=LEFT)

        sc_y = ttk.Scrollbar(frame, orient="vertical", command=self.tree.yview)
        sc_y.pack(side=RIGHT, fill=Y)
          
        self.tree["yscrollcommand"]=sc_y.set

    def content(self):
        people = [("Tom", 38, "[email protected]", "Tom", 38, "[email protected]"),
                  ("Bob", 42, "[email protected]", "Tom", 38, "[email protected]"),
                  ("Sam", 28, "[email protected]", "Tom", 38, "[email protected]"),
                  ("Tom", 38, "[email protected]", "Tom", 38, "[email protected]")]

        columns = ("name", "age", "email", "name1", "age1", "email1")

        self.tree.config(columns=columns, show="headings")

        self.tree.heading("name", text="Имя")
        self.tree.heading("age", text="Возраст")
        self.tree.heading("email", text="Email")
        self.tree.heading("name1", text="Имя")
        self.tree.heading("age1", text="Возраст")
        self.tree.heading("email1", text="Email")

        for i in columns:
            self.tree.column(i, stretch=NO)

        for person in people:
            self.tree.insert("", END, values=person)


if __name__ == '__main__':
    A = Main()
    A.run()

I have already reviewed all the monuals, tutirials. No one writes about this problem.

enter image description here

The change has been the order of the scrollbar declaration, so that the program execution flow shows the scrollbar in the correct position.

        # First place
        sc_y = ttk.Scrollbar(frame, orient="vertical")
        sc_y.pack(side=RIGHT, fill=Y)

        # Second place                    
        self.tree = ttk.Treeview(frame, yscrollcommand=sc_y.set)
        self.tree.pack(fill = BOTH, expand=1, side=LEFT)
  • Related