やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Pythonのtkinter.ttk.Treeviewでツリー表示

 木構造GUI

成果物

コード

#!/usr/bin/env python3
# coding: utf-8
import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title('Application')
root.minsize(640, 480)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

tree = ttk.Treeview(root)
tree.insert('', 0, 'item1', text='item1')
tree.insert('', 'end', 'item2', text='item2', open=True) 
tree.insert('item2', 'end', text='item21')
tree.insert('item2', 'end', text='item22')
tree.insert('', 'end', text='item3')
tree.grid(sticky="SNEW")
tree.focus(tree.identify_row(0))
tree.focus_set()
tree.selection_set(tree.identify_row(0))
root.mainloop()

実行結果

./tree.py

f:id:ytyaru:20191003132128p:plainf:id:ytyaru:20191003132131p:plain

 キーボードの上下で移動。左右で開閉。

問題

  • 一番上のヘッダ行っぽいヤツが邪魔

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux