43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
|
|
import tkinter as tk
|
||
|
|
from tkinter import ttk
|
||
|
|
from PIL import Image, ImageTk # Pillow je nutné mít nainstalované
|
||
|
|
|
||
|
|
def main():
|
||
|
|
root = tk.Tk()
|
||
|
|
root.title("Treeview s checkboxy")
|
||
|
|
|
||
|
|
tree = ttk.Treeview(root)
|
||
|
|
tree.pack(fill="both", expand=True)
|
||
|
|
|
||
|
|
# Funkce pro načtení a zmenšení obrázku na 16x16
|
||
|
|
def load_icon(path):
|
||
|
|
img = Image.open(path)
|
||
|
|
img = img.resize((16, 16), Image.Resampling.LANCZOS)
|
||
|
|
return ImageTk.PhotoImage(img)
|
||
|
|
|
||
|
|
unchecked = load_icon("/home/honza/Documents/Tagger/src/images/32/32_unchecked.png")
|
||
|
|
checked = load_icon("/home/honza/Documents/Tagger/src/images/32/32_checked.png")
|
||
|
|
|
||
|
|
# Slovník pro ukládání stavů
|
||
|
|
states = {}
|
||
|
|
|
||
|
|
# Přidání uzlu se stavem
|
||
|
|
item = tree.insert("", "end", text="Položka 1", image=unchecked)
|
||
|
|
states[item] = False
|
||
|
|
|
||
|
|
# Funkce pro přepnutí checkboxu
|
||
|
|
def toggle(event):
|
||
|
|
region = tree.identify("region", event.x, event.y)
|
||
|
|
if region == "tree":
|
||
|
|
item_id = tree.identify_row(event.y)
|
||
|
|
if item_id:
|
||
|
|
states[item_id] = not states[item_id]
|
||
|
|
tree.item(item_id, image=checked if states[item_id] else unchecked)
|
||
|
|
|
||
|
|
tree.bind("<Button-1>", toggle)
|
||
|
|
|
||
|
|
root.mainloop()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|