ragardner / tksheet

Python tkinter table widget for displaying tabular data
https://pypi.org/project/tksheet/
MIT License
404 stars 50 forks source link

How to make the Checkbox remain checked after searching in Entry #231

Closed AnonymousVibrate closed 5 months ago

AnonymousVibrate commented 5 months ago

I have this code which will update and apply the checkbox after searching, I have problem if I search the first "apple". Im getting error :

import pandas as pd
from customtkinter import CTk, CTkEntry, CTkButton
from tksheet import Sheet
from tkinter import TOP, BOTH, X

# Sample data
Check = pd.DataFrame([' '])
sample_data = pd.DataFrame(['Apple', 'Banana', 'Carrots', 'Pumpkin', 'Watermelon', 'Coconut', 'Strawberry'])
data = pd.concat([Check, sample_data], axis=1).fillna('')

# Dictionary to keep track of checkbox states
checkbox_states = {i: False for i in range(len(data))}
print(checkbox_states)
def update_checkbox_states(sheet):
    for row_idx in range(sheet.total_rows()):
        if sheet.get_cell_data(row_idx, 0) == True:
            checkbox_states[row_idx] = True
        else:
            checkbox_states[row_idx] = False
    print('DONE UPDATE')
def apply_checkbox_states(sheet):
    for row_idx, state in checkbox_states.items():
        if state:
            sheet.set_cell_data(row_idx, 0, True)
        else:
            sheet.set_cell_data(row_idx, 0, False)
    print('DONE APPLY')
def searching(entry, sheet):
    key = entry.get().lower()
    for row_idx, row in enumerate(data.values.tolist()):
        isData = any(key in str(cell).lower() for cell in row)
        if isData:
            sheet.show_rows(row_idx, redraw=True)
        else:
            sheet.hide_rows(row_idx, data_indexes=True)
    print('DONE SEARCH')
def custom_sheet():
    tkEntry = CTkEntry(app, fg_color='white', bg_color='white', text_color='black')
    tkEntry.pack(side=TOP, fill=X, expand=True)

    sheet = Sheet(app, data=data.values.tolist())
    sheet.pack(side=TOP, fill=BOTH, expand=True)
    sheet.enable_bindings()
    sheet.checkbox('A', checked=False, redraw=False)

    tkButton = CTkButton(app, text='Search', command=lambda: [update_checkbox_states(sheet), searching(tkEntry, sheet), apply_checkbox_states(sheet)])
    tkButton.pack(side=TOP)

app = CTk()
app.config(background='white')
custom_sheet()
app.mainloop()
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1967, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 861, in callit
    func(*args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\tksheet\sheet.py", line 5244, in after_redraw
    self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=True)
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\tksheet\main_table.py", line 5445, in main_table_redraw_grid_and_text
    only_rows=[self.datarn(r) for r in range(text_start_row, text_end_row)],
               ^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\tksheet\main_table.py", line 7447, in datarn
    return r if self.all_rows_displayed else self.displayed_rows[r]
                                             ~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range.

After I search the Banana or other items first, it works perfectly. Then I can search the apple after several typing other items.

ragardner commented 5 months ago

Hello,

Thanks for your report, sorry this was happening for you, I have addressed it in version 7.2.9