I get this erro: TypeError: '<' not supported between instances of 'str' and 'int'.…
- User
I get this erro: TypeError: '<' not supported between instances of 'str' and 'int'. Solve it by saving everything as strings. Lets think step by step. Python 3.12 code: from tksheet import Sheet import tkinter as tk
class MultiSortSheet(tk.Tk): def init(self): tk.Tk.init(self) self.title("Multi-Sort tksheet Example")
self.data = [ ["Apple", 10, 2.5], ["Banana", 5, 1.0], ["Apple", 15, 3.0], ["Orange", 8, 2.0], ["Banana", 12, 2.8], ["Apple", 20, 4.0], ["Orange", 10, 2.5], ] self.headers = ["Fruit", "Quantity", "Price"] self.sort_columns = [] self.sheet = Sheet(self, data=self.data, headers=self.headers) self.sheet.enable_bindings("all") self.sheet.bind("<Button-1>", self.sort_column) self.sheet.pack(expand=True, fill="both") self.sheet.bind("<<SheetModified>>", self.handle_sheet_modified) def sort_column(self, event): if self.sheet.identify_region(event) != "header": return col_index = self.sheet.identify_column(event) if col_index is None: return new_sort_columns = [] ascending = True for col, asc in self.sort_columns: if col == col_index: ascending = not asc else: new_sort_columns.append((col, asc)) new_sort_columns.insert(0, (col_index, ascending)) self.sort_columns = new_sort_columns self.multi_sort(self.sort_columns) self.sheet.set_sheet_data(self.data) def multi_sort(self, sort_columns): def sort_key(row): return tuple(self.get_sortable_value(row[col]) for col, asc in sort_columns) self.data.sort(key=sort_key, reverse=not sort_columns[0][1]) def get_sortable_value(self, value): try: return int(value) # Try converting to integer except ValueError: try: return float(value) # Try converting to float except ValueError: return str(value) # Otherwise, treat as string def handle_sheet_modified(self, event): if "added" in event and "columns" in event["added"] and event["added"]["columns"]: self.sheet.bind("<Button-1>", self.sort_column)if name == "main": app = MultiSortSheet() app.mainloop()
- Assistant
- User
Is this really a multi sort example?. I dont see any way for the user to use multisort here.
- Assistant