Tkinter Treeview Insert Item
Another common function when using the Treeview control, in addition to deleting control items, is inserting items. The insertion is done in the same way as the insert( ) method for creating controls. As for the inserted content, you can use tkinter’s Entry control. This will be illustrated with examples.
Example#1
Add design insertion. Since this Treeview control contains two columns under the icon column, you must create two Entry controls if you want to insert. Since we have to label the inserted control, we have to add two labels next to the Entry. In addition, a button must be used to indicate the insertion operation when performing the insertion, so a separate button must be created.
from tkinter import *
from tkinter.ttk import *
def removeItem(): # Delete the selected item
ids = tree.selection() # Obtain the selected items
for id in ids: # The selected items may be many so use the loop
tree.delete(id) # Delete the selected item
def insertItem():
state = stateEntry.get() # Get input from stateEntry
city = cityEntry.get() # Get input from cityEntry
# If the input data is incomplete do not proceed
if (len(state.strip())==0 or len(city.strip())==0):
return
tree.insert("",END,text=state,values=(city)) # Insert # tree.insert("",END,text=state,values=city)
stateEntry.delete(0,END) # Delete stateEntry
cityEntry.delete(0,END) # Delete cityEntry
root = Tk()
root.title("apidemos.com")
stateCity = {"Illinois": "Chicago", "California": "Los Angeles",
"Texas": "Houston", "Washington": "Seattle",
"Jiangsu": "Nanjing", "Shandong": "Qingdao",
"Guangdong": "Guangzhou", "Fujian": "Xiamen"}
# The following three lines are mainly applied to the zoom window
root.rowconfigure(1,weight=1) # row1 will change 1:1 with the window zoom
root.columnconfigure(1,weight=1) # column1 will change 1:1 with the window zoom
root.columnconfigure(3,weight=1) # column3 will change 1:1 with window scaling
stateLab = Label(root,text="State :") # Create State:Label
stateLab.grid(row=0,column=0,padx=5,pady=3,sticky=W)
stateEntry = Entry() # Create State:Entry
stateEntry.grid(row=0,column=1,sticky=W+E,padx=5,pady=3)
cityLab = Label(root,text="City : ")
cityLab.grid(row=0,column=2,sticky=E)
cityEntry = Entry()
cityEntry.grid(row=0,column=3,sticky=W+E,padx=5,pady=3)
# Create an Insert button
inBtn = Button(root,text="Insert",command=insertItem)
inBtn.grid(row=0,column=4,padx=5,pady=3)
# Create a Treeview with multiple selections selectmode=EXTENDED
tree = Treeview(root,columns=("cities"),selectmode=EXTENDED)
# Create column headings
tree.heading("#0",text="State") # Icon bar
tree.heading("cities",text="City")
# Format field
tree.column("cities",anchor=CENTER)
# Create content
for state in stateCity.keys():
tree.insert("",index=END,text=state,values=stateCity[state])
tree.grid(row=1,column=0,columnspan=5,padx=5,sticky=W+E+N+S)
rmBtn = Button(root,text="Delete",command=removeItem) # "Delete" button
rmBtn.grid(row=2,column=2,padx=5,pady=3,sticky=W)
root.mainloop()
Output:
The first parameter in the rowconfigure( ) method in line 26 is 1, representing row=1, which is equivalent to letting the Treeview control with row=1 scale with the window, and the scaling ratio is 1:1 scaled by the second parameter weight=1. The first parameter of the columnconfigure( ) method in line 27 is 1, representing column=1, which is equivalent to letting the stateEntry control with column=1 scale with the window, and the scaling ratio is 1:1 scaled by the second parameter weight=1. The first parameter in the columnconfigure( ) method in line 28 is 3, which stands for column=3, which is equivalent to letting the cityEntry control with column=3 scale with the window, and the scaling ratio is 1:1 scaled by the second parameter weight=1. Without the above setting, when scaling the window, all components will not change their size.
Lines 39 and 40 create the "Insert" button, which when clicked will execute the insertItem( ) method on lines 8 to 16. In this method, line 9 reads the input from stateEntry and line 10 reads the input from cityEntry. Lines 12 and 13 are to determine if there is input in both columns, and if there is no input in one column, then return and do not proceed further. Line 14 is to insert the input of stateEntry and cityEntry. Since the insertion has been completed, line 15 deletes the content of the stateEntry text box and line 16 deletes the content of the cityEntry text box.