ragardner / tksheet

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

How to accept user input even if "enter" is not pressed #212

Closed joestraw265 closed 8 months ago

joestraw265 commented 8 months ago

Hello!

First and foremost, congratulations for the amazing job!

My question is simple (I guess) but I have been trying to circumvent this issue for 2 days without any success.

I have a sheet in a tkinter GUI where the user is supposed to input values manually. I have a tk button that calls a function that makes several computations with the values given by the user. The problem lies with the last value given by the user. If the user presses "enter", or clicks anywhere on the table, no worries. The cell's edit ends and the value is stored. However, if after writing the last value in the last cell, the user immediately presses the button without "finalizing" the editing (so to speak. i.e. not clicking anywhere inside the table or pressing "enter"), the value will not be stored and the computations won't be done.

PS1: If the user presses the button a 2nd time, everything works again

PS2: This didn't happen when I first wrote my code (almost 2 years ago... I was updating it now and making the necessary changes to be aligned with your newest version)

I am using Python 3.11.5 and tksheet 7.0.5

Many thanks in advance!

ragardner commented 8 months ago

Hello, thanks for your report and sorry to hear that the behaviour has changed.

It's difficult for me know what's going on at the moment, would it be possible to get just the lines that involve the sheet, specifically:

You can strip out anything sensitive and the code does not have to run/work

joestraw265 commented 8 months ago

Hello,

Thanks for the quick response.

Here are the chunks of the code. I know it is pretty rudimentary, but bear with me :P

The code for the button:

button = Button(frame, text="Compute", command=lambda: compute())
button.grid(row=6, column=0, columnspan=3)

The sheet is initialized like this:

sheet_in_AN = Sheet(frame, align="center", empty_horizontal=0, empty_vertical=0, show_top_left=False,   
                                   cell_auto_resize_enabled = False, show_row_index=False, show_header=False, width=661, height=208, 
                                   show_x_scrollbar=False)
sheet_in_AN.grid(row=2, column=3, columnspan=5,rowspan=7)
sheet_in_AN.set_sheet_data([["" for cj in range(4)] for ri in range(20)])
sheet_in_AN.set_column_widths(column_widths=[88,198,174,174])
sheet_in_AN.enable_bindings(("single_select", "drag_select","arrowkeys",
                                                 "right_click_popup_menu","copy","cut",
                                                  "paste","delete","undo","edit_cell"))

Inside the function compute(), the lines that go fetch the values input by the user:

col3 = sheet_in_AN["C"].data
col4 = sheet_in_AN["D"].data

The values in columns A and B are automatically filled by other function. The user only inputs data in columns C and D.

What usually happens is that the users fill all the blank cells in columns C and D, but after writing the last value in column D, they don't press "enter" or click anywhere else on the sheet. Instead, they normally click directly on the button and the last cell filled in D column never ceases to edit, therefore the value they input is never stored.

So, col3 list's length is 1 value higher than the col4 and the rest of the compute() function, that needs that last value, fails to run on that try and the computations aren't done.

What is funny is that the first click on the button takes the focus out of the sheet widget, but it doesn't store the value, so the function doesn't run. But if I click a second time, the value is stored (probably after the first run of the function) and it works...

ragardner commented 8 months ago

Hello,

Thanks for the extra detail,

I reckon the issue is partly related to this

Where a tk button doesn't receive focus upon clicking on it and so "<FocusOut>" is not triggered

What I've done in tksheet version 7.0.6 is added two functions:

This is so that in your compute() function you can add at the start of the function the line:

sheet_in_AN.close_text_editor()

This should set the cells value as if the user has hit Return

I am not sure why it was previously working okay with the older version though, perhaps I had bound more events to trigger the closing of the editor

Let me know if this is any help, if not I will try a different strategy

joestraw265 commented 8 months ago

Hello again,

I must say it worked like a charm!

Thank you so much for the impeccable assistance you have provided me and for the amazing work with developing this tool.

Keep up the awesome job, it is really uncanny how useful tksheet in our community!