@@ -51,6 +51,8 @@ def __init__(self, parent: tk.Tk):
51
51
instruction_label = ttk .Label (self .main_frame , text = instruction_text , font = ('Arial' , 12 ))
52
52
instruction_label .pack (pady = (10 , 20 ))
53
53
54
+ self .sort_column = None
55
+
54
56
style = ttk .Style (self .root )
55
57
# Add padding to Treeview heading style
56
58
style .layout ("Treeview.Heading" , [
@@ -76,11 +78,14 @@ def __init__(self, parent: tk.Tk):
76
78
values = (key ,) + tuple (getattr (template_overview , attr , '' ) for attr in attribute_names )
77
79
self .tree .insert ('' , 'end' , text = key , values = values )
78
80
79
- self .adjust_treeview_column_widths ()
81
+ self .__adjust_treeview_column_widths ()
80
82
81
- self .tree .bind ('<Double-1>' , self .on_row_double_click )
83
+ self .tree .bind ('<Double-1>' , self .__on_row_double_click )
82
84
self .tree .pack (fill = tk .BOTH , expand = True )
83
85
86
+ for col in self .tree ["columns" ]:
87
+ self .tree .heading (col , text = col , command = lambda col = col : self .__sort_by_column (col , False ))
88
+
84
89
if isinstance (self .root , tk .Toplevel ):
85
90
try :
86
91
while self .root .children :
@@ -91,7 +96,7 @@ def __init__(self, parent: tk.Tk):
91
96
else :
92
97
self .root .mainloop ()
93
98
94
- def adjust_treeview_column_widths (self ):
99
+ def __adjust_treeview_column_widths (self ):
95
100
"""
96
101
Adjusts the column widths of the Treeview to fit the contents of each column.
97
102
"""
@@ -109,14 +114,33 @@ def adjust_treeview_column_widths(self):
109
114
# Update the column's width property to accommodate the largest text width
110
115
self .tree .column (col , width = int (max_width * 0.6 + 10 ))
111
116
112
- def on_row_double_click (self , event ):
117
+ def __on_row_double_click (self , event ):
113
118
"""Handle row double-click event."""
114
119
item_id = self .tree .identify_row (event .y )
115
120
if item_id :
116
121
selected_template_relative_path = self .tree .item (item_id )['text' ]
117
122
ProgramSettings .store_template_dir (selected_template_relative_path )
118
123
self .root .destroy ()
119
124
125
+ def __sort_by_column (self , col , reverse : bool ):
126
+ if self .sort_column and self .sort_column != col :
127
+ self .tree .heading (self .sort_column , text = self .sort_column )
128
+ self .tree .heading (col , text = col + (' ▼' if reverse else ' ▲' ))
129
+ self .sort_column = col
130
+
131
+ try :
132
+ col_data = [(float (self .tree .set (k , col )), k ) for k in self .tree .get_children ('' )]
133
+ except ValueError :
134
+ col_data = [(self .tree .set (k , col ), k ) for k in self .tree .get_children ('' )]
135
+ col_data .sort (reverse = reverse )
136
+
137
+ # rearrange items in sorted positions
138
+ for index , (_val , k ) in enumerate (col_data ):
139
+ self .tree .move (k , '' , index )
140
+
141
+ # reverse sort next time
142
+ self .tree .heading (col , command = lambda : self .__sort_by_column (col , not reverse ))
143
+
120
144
121
145
def argument_parser ():
122
146
"""
0 commit comments