Tabela: Treeview
Widżety ttk: Combobox, Treeview i Notebook - Aplikacje okienkowe w Pythonie: tkinter krok po kroku (Python)
ttk.Treeview z opcją show="headings" to tabela. Kolumny nazywa columns, nagłówki ustawia heading(kolumna, text=...), szerokość column(kolumna, width=...). Wiersz dodaje insert("", tk.END, values=(...)), a get_children() zwraca identyfikatory wszystkich wierszy. Wartości wiersza odczytuje item(id, "values").import tkinter as tk
from tkinter import ttk
GRADES = [("Ola", 5), ("Jan", 4), ("Ewa", 6), ("Piotr", 3)]
root = tk.Tk()
root.title("Oceny")
table = ttk.Treeview(root, columns=("name", "grade"), show="headings", height=5)
table.heading("name", text="Uczeń")
table.heading("grade", text="Ocena")
table.column("grade", width=80, anchor="center")
for row in GRADES:
table.insert("", tk.END, values=row)
table.pack(padx=10, pady=10)
grades = [int(table.item(i, "values")[1]) for i in table.get_children()]
ttk.Label(root, text=f"Średnia: {sum(grades) / len(grades):.2f}").pack(pady=(0, 10))
root.mainloop()Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.
Wszystkie lekcje kursu Aplikacje okienkowe w Pythonie: tkinter krok po kroku·Zaloguj się, aby zapisać postęp na koncie.