tksheet
tksheet copied to clipboard
REQUEST : Add a sort function
Can we add a functionality that sort the column when you clicked the header ?
import pandas as pd
from tksheet import Sheet
from customtkinter import CTk
from tkinter import TOP, BOTH, X
data = pd.DataFrame(['Banana', 'Apple', 'Carrots', 'Pumpkin', 'Watermelon', 'Coconut', 'Strawberry'])
def custom_sheet():
sheet = Sheet(app, data=data.values.tolist())
sheet.pack(side=TOP, fill=BOTH, expand=True)
sheet.enable_bindings()
sheet.headers(['Sort this column']) #add additional settings to enable header clickable right side arrow to sort values.
app = CTk()
custom_sheet()
app.mainloop()
Id like to add this as well. Maybe at least start with a straight forward way to sort a sheet by a single column in the sheet. That could then be exposed/used without a custom approach every time a user wants to sort a sheet. Something similar could be done for filtering a sheet. using just a single column initially
I am looking into this due to the number of requests, if you have any further tips or advice let me know!
Thank you. Coffee Sent!
Some benefit might be had by adding in a few more updates to the docs. If i can i'll bounce a few of my implementations off of you so maybe they can get incorporated as you break it out.
On Thu, Feb 6, 2025 at 11:15 AM ragardner @.***> wrote:
I am looking into this due to the number of requests, if you have any further tips or advice let me know!
— Reply to this email directly, view it on GitHub https://github.com/ragardner/tksheet/issues/238#issuecomment-2640289328, or unsubscribe https://github.com/notifications/unsubscribe-auth/BPHJULTCTOEG4IGCGI7KYWD2OODDNAVCNFSM6AAAAABWT2IVNCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNBQGI4DSMZSHA . You are receiving this because you commented.Message ID: @.***>
I am looking into this due to the number of requests, if you have any further tips or advice let me know!
Hey, I somehow managed to do a sorting of data in tksheet:
import tkinter as tk
import pandas as pd
from tksheet import Sheet
from tkinter import messagebox
# Sample data
data = [
["Alice", "Smith", "Math", 20],
["Bob", "Johnson", "Science", 18],
["Charlie", "Brown", "History", 22],
["David", "Wilson", "Math", 19],
["Emma", "Moore", "Science", 21]
]
columns = ["First Name", "Last Name", "Course", "Age"]
# Track sorting order
sort_order = {}
def sort_table(col):
# Get the index of the selected column
selected_column_index = col['selected'].column
headers = sheet.headers()
selected_header = headers[selected_column_index]
answer = messagebox.askyesno(f"Confirmation", f"Sort by {selected_header}?", parent= root)
if answer:
# Retrieve all data from the table
full_data = sheet.get_sheet_data()
# Convert the full data to a DataFrame for easier sorting
df_full = pd.DataFrame(full_data, columns=headers)
# Sort the full DataFrame by the selected column
sorted_df = df_full.sort_values(by=selected_header, ascending=True)
# Update the table with the sorted data
sheet.set_sheet_data(sorted_df.values.tolist())
#Enable bindings
sheet.enable_bindings("single_select",
"row_select",
"column_width_resize",
"column_select",
"copy",
"up",
"down",
)
# Refresh the table to apply changes
sheet.refresh()
# Create Tkinter window
root = tk.Tk()
root.title("TkSheet Sorting Example")
root.geometry("600x400")
# Create tksheet
sheet = Sheet(root, data=data, headers=columns)
sheet.enable_bindings("all")
sheet.pack(fill="both", expand=True)
# Bind click event for sorting when column headers are clicked
sheet.extra_bindings('column_select', sort_table)
# Run Tkinter main loop
root.mainloop()
Hello, sorting is coming soon I am just finishing the latest version which will hopefully be released within the next 5 days, changes include:
- make the treeview have full table functionality including drag and drop, insert rows, delete rows, undo, redo and sorting
- cell text wrapping, character or word
- Sheet.sorting functions which accept a key (default is a natural sorting key which has the aim of try to handle any python object) and reverse arguments, including:
- sorting a box of cells
- sorting values in rows
- sorting values in columns
- re-ordering rows based on the values of a column
- re-ordering columns based on the values of a row
If you can think of any other sorting functions you would like to see let me know
Awesome, thanks for the update!
I am still new to tksheet so this may already be available, but if not would probably be use if the view to a sheet is set a certain way, maybe it maintains that view while performing the sort. For example, its often useful to sort a subset of the entire sheet, like when a filter is applied. If it's easy to perform the sort in a manner that does not reset the sheet view I think that would be very common use too. Thank you for such a useful package.
On Fri, Feb 14, 2025 at 12:50 PM ragardner @.***> wrote:
Hello, sorting is coming soon I am just finishing the latest version which will hopefully be released within the next 5 days, changes include:
- make the treeview have full table functionality including drag and drop, insert rows, delete rows, undo, redo and sorting
- cell text wrapping, character or word
- Sheet.sorting functions which accept a key (default is a natural sorting key which has the aim of try to handle any python object) and reverse arguments, including:
- sorting a box of cells
- sorting values in rows
- sorting values in columns
- re-ordering rows based on the values of a column
- re-ordering columns based on the values of a row
If you can think of any other sorting functions you would like to see let me know
— Reply to this email directly, view it on GitHub https://github.com/ragardner/tksheet/issues/238#issuecomment-2659933103, or unsubscribe https://github.com/notifications/unsubscribe-auth/BPHJULTH2CH6J3AXFEDNFY32PYUFPAVCNFSM6AAAAABWT2IVNCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNJZHEZTGMJQGM . You are receiving this because you commented.Message ID: @.***> [image: ragardner]ragardner left a comment (ragardner/tksheet#238) https://github.com/ragardner/tksheet/issues/238#issuecomment-2659933103
Hello, sorting is coming soon I am just finishing the latest version which will hopefully be released within the next 5 days, changes include:
- make the treeview have full table functionality including drag and drop, insert rows, delete rows, undo, redo and sorting
- cell text wrapping, character or word
- Sheet.sorting functions which accept a key (default is a natural sorting key which has the aim of try to handle any python object) and reverse arguments, including:
- sorting a box of cells
- sorting values in rows
- sorting values in columns
- re-ordering rows based on the values of a column
- re-ordering columns based on the values of a row
If you can think of any other sorting functions you would like to see let me know
— Reply to this email directly, view it on GitHub https://github.com/ragardner/tksheet/issues/238#issuecomment-2659933103, or unsubscribe https://github.com/notifications/unsubscribe-auth/BPHJULTH2CH6J3AXFEDNFY32PYUFPAVCNFSM6AAAAABWT2IVNCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNJZHEZTGMJQGM . You are receiving this because you commented.Message ID: @.***>
Hello,
Sorting functionality has now been added in version 7.4.0 along with many other changes
https://github.com/ragardner/tksheet/wiki/Version-7#sorting-the-table
Let me know if you think there should be adjustments or additional functionality 😊
The latest version is now 7.4.2, some issues with sorting have been ironed out.
Hi I am trying to use the sorting as AnonymousVibrate described where you click the header to sort the rows by values in the selected column. I can't work out how to bind the sort function to the header click any help would be appreciated