ragardner / tksheet

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

Cell Selection Persistence #219

Closed adam-ring closed 5 months ago

adam-ring commented 5 months ago

I have a largish grid (64x64) that is always visible with no scrolling. None of the cell data can be changed directly, but I am using popup menus to perform commands on selected cells. My issue is that the cell selection persists as I select new cells. My expectation is that previously selected cells would only stay selected with additional left button mouse clicks if Ctrl is pressed, however, no keyboard keys are being pressed. I can select columns, cells, groups of cells using Shift, etc., but all of the previous cells stay highlighted. There are some very infrequent times when the cell selections do properly get reset, but I have not been able to determine any changes in input that may cause that change in result.

Here is the object creation within a custom ttk.Frame class:

from tkinter import ttk
class PatternFrame(ttk.Frame):
    num_rows = 64
    num_cols = 64

    def __init__(
        self, master: T_MasterFrame, width: int = 1120, height: int = 560, **kwargs
    ) -> None:
        super().__init__(master, **kwargs)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.sheet = tksheet.Sheet(
                self,
                width=width,
                height=height,
                show_header=True,
                show_top_left=False,
                show_row_index=True,
                show_x_scrollbar=False,
                show_y_scrollbar=False,
                default_header="numbers",
                header_font=("Arial", 4, "normal"),
                font=("Arial", 4, "normal"),
                data=np.full((self.num_rows, self.num_cols), "").tolist(),
            )
        self.sheet.set_all_column_widths(17)
        self.sheet.set_all_row_heights(8.5)

        self.sheet.enable_bindings(
            (
                "single_select",
                "toggle_select",
                "drag_select",
                "column_select",
                "row_select",
                "up",
                "down",
                "left",
                "right",
                "rc_select",
                "right_click_popup_menu",
                "ctrl_select",
                "copy",
                "paste",
                "undo",
            )
        )

I have been using self.sheet.deselect() in some callbacks, and that does work to clear the selections. I would like to know if it is possible to get closer to standard mouse-click selection operation.

This is an issue on both a Raspberry Pi 4 machine (Python 3.9.2) and Windows 11 (Python 3.9.19). tksheet 7.1.7

I think that this problem was felt on previous versions of tksheet as well, but I have not double-checked.

BTW, I need to include that catch_warnings to avoid the following warning every time a new sheet is created. Is there any different way to handle that?

C:\Users\<USER>\Miniconda3\envs\fs_demo\lib\site-packages\tksheet\sheet.py:275: UserWarning: There have been many changes from tksheet version 6.x.x to version 7.x.x. Please see the changelog for more information.
  WARNING(
ragardner commented 5 months ago

Hello,

Thanks for your report,

I think removing this line will solve the persistent selections issue:

"toggle_select",

If you experience further selection issues after removing the line please don't hesitate to report them

As for the warnings, I should have perhaps added a method of disabling them sorry. The method you have employed to remove them seems like the best possible.

I will remove the warning in the next update as I reckon enough time has passed.

adam-ring commented 5 months ago

That did indeed fix the issue. Thanks for the quick response! 👊