ragardner / tksheet

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

Issue with set_sheet_data #201

Closed mithoon19 closed 10 months ago

mithoon19 commented 11 months ago

Hello, good day.

First of all, Thank you for your amazing work.

I have a table and I keep refreshing it with new data. I use set_sheet_data to update data in table. I use below code to update sheet.

self.sheet.set_sheet_data(data=newdata, reset_col_positions=False)

Above code works most of the time. But, sometimes, I get below error and after which new values does not reflect automatically in my table. Table data gets updated with new values but does not get displayed. New updated values are displayed only after I click some cell.

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\catuser\AppData\Local\Programs\Python\Python310\lib\tkinter__init.py", line 1921, in call return self.func(*args) File "C:\Users\catuser\AppData\Local\Programs\Python\Python310\lib\tkinter\init__.py", line 839, in callit func(*args) File "C:\Users\catuser\OneDrive\proj\lib\site-packages\tksheet_tksheet.py", line 383, in after_redraw self.MT.main_table_redraw_grid_and_text(redraw_header=redraw_header, redraw_row_index=redraw_row_index) File "C:\Users\catuser\OneDrive\proj\lib\site-packages\tksheet_tksheet_main_table.py", line 5461, in main_table_redraw_grid_and_text self.CH.redraw_grid_and_text( File "C:\Users\catuser\OneDrive\proj\lib\site-packages\tksheet_tksheet_column_headers.py", line 1626, in redraw_grid_and_text iid, showing = self.hidd_text[k].pop() KeyError: 'pop from an empty set'

I tested with delete_rows and then calling set_sheet_data. But, again received error sometimes.

self.sheet.delete_rows()
self.sheet.set_sheet_data(data=newdata, reset_col_positions=False)

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\catuser\AppData\Local\Programs\Python\Python310\lib\tkinter__init.py", line 1921, in call return self.func(*args) File "C:\Users\catuser\AppData\Local\Programs\Python\Python310\lib\tkinter\init__.py", line 839, in callit func(*args) File "C:\Users\catuser\OneDrive\proj\lib\site-packages\tksheet_tksheet.py", line 383, in after_redraw self.MT.main_table_redraw_grid_and_text(redraw_header=redraw_header, redraw_row_index=redraw_row_index) File "C:\Users\catuser\OneDrive\proj\lib\site-packages\tksheet_tksheet_main_table.py", line 5351, in main_table_redraw_grid_and_text iid, showing = self.hidd_text[k].pop() KeyError: 'pop from an empty set'ty set'

I use below code to initialize my sheet.

self.sheet = Sheet(self.table_frame,
                            show_top_left=False,
                            show_row_index=False,
                            show_x_scrollbar=False,
                            show_y_scrollbar=False,
                            empty_horizontal=0,
                            empty_vertical=0,
                            headers=['1', '2', '3', '4', '5', '6', '7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22'],
                            theme="dark",
                            width=800,
                            height=200,
                            align="c")

self.sheet.enable_bindings("single_select")
self.sheet.set_column_widths([200, 80, 80, 80, 120, 100, 100, 100, 70, 70, 70, 70, 60, 70, 70, 70, 70, 70, 70, 50, 50, 50])
self.sheet.grid(row=0, rowspan=4, column=0, padx=5, pady=(10, 0), sticky="nswe")
ragardner commented 11 months ago

Hello and thank you very much for your report, I suspect this is a bug and I'll get back to you when I have fixed it

ragardner commented 11 months ago

@mithoon19 I am having some trouble reproducing the error myself, is there a way you could provide me a minimal example?

I am currently using the below code on tksheet version 6.2.5

# ruff: noqa

from tksheet import *
import tkinter as tk
import random

class demo(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.frame = tk.Frame(self)
        self.frame.grid_columnconfigure(0, weight=1)
        self.frame.grid_rowconfigure(0, weight=1)

        self.sheet = Sheet(self.frame,
                            show_top_left=False,
                            show_row_index=False,
                            show_x_scrollbar=False,
                            show_y_scrollbar=False,
                            empty_horizontal=0,
                            empty_vertical=0,
                            headers=['1', '2', '3', '4', '5', '6', '7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22'],
                            theme="dark",
                            width=800,
                            height=200,
                            align="c")
        self.sheet.set_column_widths([200, 80, 80, 80, 120, 100, 100, 100, 70, 70, 70, 70, 60, 70, 70, 70, 70, 70, 70, 50, 50, 50])
        self.sheet.enable_bindings("all", "edit_index", "edit header", "ctrl_select")
        self.sheet.popup_menu_add_command(
            "test",
            self.test,
            table_menu=True,
            header_menu=True,
            empty_space_menu=True,
        )

        self.frame.grid(row=0, column=0, sticky="nswe")
        self.sheet.grid(row=0, column=0, sticky="nswe")

    def test(self, event=None):
        t = random.randint(0, 500)
        newdata = [[f"{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n" for c in range(22)] for r in range(30)]
        self.sheet.headers([f"{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n{t}\n" for c in range(22)])
        self.sheet.set_sheet_data(data=newdata, reset_col_positions=False)

app = demo()
app.mainloop()
mithoon19 commented 11 months ago

Thank you for quick response. I unable to reproduce the error. It just happens randomly. Most of the time it works just perfect. So, I am unable to figure out what could be the issue.

ragardner commented 11 months ago

Ok no worries in that case, just to check before I make some changes sorry, which version of tksheet are you using?

mithoon19 commented 11 months ago

I am using Version: 6.2.5

ragardner commented 11 months ago

@mithoon19 The issue should be totally fixed in version 6.2.6, release notes:

Version 6.2.6

Fixed:

Added:

But please let me know if you encounter any issues!

Thanks for your help