Um die Spalten sortierbar zu machen, musst du die Methode sort auf den Treeview anwenden, um die Sortierungsfunktionen für jede Spalte zu definieren.
import tkinter as tk
from tkinter import ttk
class GridViewApp:
def __init__(self, root):
self.root = root
self.root.title("GridView Beispiel")
self.gridview = ttk.Treeview(self.root, columns=("Column 1", "Column 2", "Column 3"), show="headings")
self.gridview.heading("Column 1", text="Spalte 1", command=lambda: self.sort_column("Column 1"))
self.gridview.heading("Column 2", text="Spalte 2", command=lambda: self.sort_column("Column 2"))
self.gridview.heading("Column 3", text="Spalte 3", command=lambda: self.sort_column("Column 3"))
self.gridview.pack()
self.populate_grid()
def populate_grid(self):
for i in range(1, 10):
self.gridview.insert("", "end", values=(f"Zeile {i}", f"Wert {i*2}", f"{i+5}"))
def sort_column(self, column):
data = [(self.gridview.set(child, column), child) for child in self.gridview.get_children("")]
data.sort(reverse=False) # Set reverse=True for descending order
for index, item in enumerate(data):
self.gridview.move(item[1], "", index)
def main():
root = tk.Tk()
app = GridViewApp(root)
root.mainloop()
if __name__ == "__main__":
main()