Python - Tkinter Scrollbar Ekleyemiyorum!

Merhaba,

Tablo, oluşturmak istiyorsanız, Treeview modülünü kullanmanızı öneririm.

#!/usr/bin/python3.8
# -*- coding: utf-8 -*-

import tkinter as tk

from tkinter.ttk import Treeview as Tv


class Treeview(Tv):
    def __init__(self, columns: list, values: list, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.columns = columns
        self.values = values
        self.y_scrollbar = tk.Scrollbar(
            master=self.master,
            orient="vertical"
        )
        self.configure(yscrollcommand=self.y_scrollbar.set)
        self.y_scrollbar.configure(command=self.yview)
        self.y_scrollbar.pack(side="right", fill="y")
        self.configure(
            show="headings",
            columns=[f"#{i + 1}" for i in range(len(self.columns))],
            height=20,
            selectmode="extended"
        )
        self.pack(side="left")
        for index, column in enumerate(self.columns):
            self.column(
                column=f"#{index + 1}",
                minwidth=100,
                width=200,
                anchor="center"
            )
            self._heading(col=index, text=column)
        for i in range(len(self.values[0])):
            self.insert(
                parent="", 
                index=i, 
                values=[j[i] for j in self.values]
        )
        self.bind(
            sequence="<Control-a>",
            func=lambda event: self.select_all()
        )
        self.bind(
            sequence="<Control-A>",
            func=lambda event: self.select_all()
        )

    def _heading(self, col: int, text: str):
        self.heading(
            column=f"#{col + 1}",
            text=text,
            command=lambda: self.sort(col=col, reverse=False)
        )

    def sort(self, col: int, reverse: bool):
        column = [
            (self.set(k, col), k)
            for k in self.get_children("")
        ]
        try:
            column.sort(key=lambda t: int(t[0]), reverse=reverse)
        except ValueError:
            column.sort(reverse=reverse)
        for index, (val, k) in enumerate(column):
            self.move(k, "", index)
        self.heading(
            column=col,
            command=lambda: self.sort(col=col, reverse=not reverse)
        )

    def select_all(self):
        for child in self.get_children():
            self.selection_add(child)
            

if __name__ == "__main__":           
    root = tk.Tk()
    tv = Treeview(
        master=root, 
        columns=["A Sütunu", "B Sütunu"], 
        values=[[*range(100)], [i ** 2 for i in range(100)]]
    )
    root.mainloop()
1 Beğeni