Tkinter Treeview Sorting
After creating the Treeview control, there is a very common function to sort the column information, usually you can click on the column title to perform the sorting, this section will explain this aspect of the application with examples.
Example#1
Sort the data in the State column of the Treeview control, in this program the icon column is omitted in order to simplify the program. So Treeview control has only one State column, when click on the column title can be sorted normally (from small to large), if you click again can reverse the sorting, sorting will be so switched.
from tkinter import *
from tkinter.ttk import *
def treeview_sortColumn(col):
global reverseFlag # Define sort mark global variables
lst = [(tree.set(st,col),st)
for st in tree.get_children("")]
print(lst) # Print List
lst.sort(reverse=reverseFlag) # Sort List
print(lst) # Print List
for index, item in enumerate(lst): # Re-move project content
tree.move(item[1],"",index)
reverseFlag = not reverseFlag # Changing the sort mark
root = Tk()
root.title("apidemos.com")
reverseFlag = False # False # The sort marker indicates whether or not to reverse sort
myStates = {"Illinois","California","Texas","Washington",
"Jiangsu","Shandong","Guangdong","Fujian",
"Mississippi","Kentucky","Florida","Indiana"}
tree = Treeview(root,columns=("states"),show="headings")
yscrollbar = Scrollbar(root) # y-axis scrollbar object
yscrollbar.pack(side=RIGHT,fill=Y) # y-axis scrollbar pack display
tree.pack()
yscrollbar.config(command=tree.yview) # y-axis scrollbar setting
tree.configure(yscrollcommand=yscrollbar.set)
# Create column headings
tree.heading("states",text="State")
# Create content
for state in myStates:
tree.insert("",index=END,values=state) # tree.insert("",index=END,values=(state,))
# Clicking on the title bar will launch the treevi_sortColumn
tree.heading("#1",text="State",
command=lambda c="states": treeview_sortColumn(c))
# tree.heading("#1",text="State",command=lambda : treeview_sortColumn("states"))
root.mainloop()
Output:
This program omits the icon bar for simplicity, and adds the show="headings" parameter when creating the Treeview control on line 23.
tree = Treeview(root,columns=("states"),show="headings")
Lines 32 and 33 are to create the data for the columns, which is equivalent to putting the myStates list data into the Treeview control. The next lines 34 and 35 are the focus, which is actually the heading( ) method, so it is a command, just because it is too long divided into two lines of writing. This is a Lambda expression that sets "states" to the variable c and then passes c as a parameter to the treeview_sortColumn( ) method.
Lines 4 to 13 of the program are the treeview_sortColumn( ) method. In this method, the current list contents are listed in lines 8 and 10 in order to let the reader understand the data content, so that the reader can understand the meaning of the current program. First of all, line 5 sets reverseFlag as a global variable, and its original definition is in line 17. Lines 6 and 7 are actually the same command, as shown below.
lst = [(tree.set(st,col),st)
for st in tree.get_children("")]
The above has a get_children( ) method, which has the following syntax.
get_children([item])
It will pass back the iid value of a tuple of item, if omitted it is to get a tuple, this tuple is the iid value of top-level.
The above program mainly creates the lst list, and line 8 will print the contents of this list, which can be seen in the Python Shell window, as follows.
[('Shandong', 'I001'), ('Indiana', 'I002'), ('Kentucky', 'I003'), ('Texas', 'I004'), ('Fujian', 'I005'), ('Florida', 'I006'), ('Guangdong', 'I007'), ('Jiangsu', 'I008'), ('Mississippi', 'I009'), ('California', 'I00A'), ('Washington', 'I00B'), ('Illinois', 'I00C')]
Line 9 is to sort the contents of the above list, and line 10 is to list the result of the sorting, as shown below.
[('California', 'I00A'), ('Florida', 'I006'), ('Fujian', 'I005'), ('Guangdong', 'I007'), ('Illinois', 'I00C'), ('Indiana', 'I002'), ('Jiangsu', 'I008'), ('Kentucky', 'I003'), ('Mississippi', 'I009'), ('Shandong', 'I001'), ('Texas', 'I004'), ('Washington', 'I00B')]
Lines 11 and 12 read as follows.
for index, item in enumerate(lst): # Re-move project content
tree.move(item[1],"",index)
There is a move( ) method with the following syntax.
remove(iid, parent, index)
Move the item referred to by iid to the index position of the parent hierarchy. This program uses "" to represent the parent hierarchy. Line 13 is to change the sort identifier so that the reverse sort can be used next time.