Tkinter Create a hierarchical Treeview
The knowledge of Hierarchy has been introduced in the previous sections, now the reader just needs to create the top-level project id in the icon bar first, and then place the related sub-projects in the top-level project id to which they belong.
Example#1
Create hierarchical Treeview control content.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
asia = {"China": "Beijing", "Japan": "Tokyo", "Thailand": "Bangkok", "South Korea": "Seoul"}
euro = {"UK": "London", "France": "Paris", "Germany": "Berlin", "Norway": "Oslo"}
# Create Treeview
tree = Treeview(root,columns=("capital"))
# Create column headings
tree.heading("#0",text="Country") # Icon bar
tree.heading("capital",text="Capital")
# Create id
idAsia = tree.insert("",index=END,text="Asia")
idEuro = tree.insert("",index=END,text="Europe")
# Create the content under idAsia
for country in asia.keys():
tree.insert(idAsia,index=END,text=country,values=asia[country])
# Create the content under idEuro
for country in euro.keys():
tree.insert(idEuro,index=END,text=country,values=euro[country])
tree.pack()
root.mainloop()
Output:
In the above program lines 8 and 9 are to create the dictionary data of Asiaasia and Europeeuro countries and capitals. The most critical thing is to use the insert( ) method, the first parameter must be placed under the id of the top-level. lines 20 and 21 are to establish the country name and capital data of Asian countries, so the insert( ) method in line 21 method of the first parameter is idAsia, which means that the inserted data is placed under the idAsia level, the program code design is as follows.
tree.insert(idAsia,...)
Lines 23 and 24 are to create European country names and capital city data, so the first parameter of the insert( ) method in line 24 is idEuro, which means that the inserted data is placed under the idEuro hierarchy, and the program code is designed as follows.
tree.insert(idEuro,...)