Pysimplegui: [ Bug] Table SetFocus() does not acknowledge Keyboard cursor after Table Update with values

Created on 17 Oct 2019  路  8Comments  路  Source: PySimpleGUI/PySimpleGUI

SetFocus with table does not acknowledge Keyboard cursor movement after Table Update using values

Problem Description

Mouse click on a row within an active Table Element. That row highlights. Use the up/down arrow key to navigate through the table. If you perform a table update (using value) then arrow keys no longer control the table even after SetFocus(force=True) unless you manually mouse click on the table again.

Problem Summary

I can demonstrate the problem with PySimpleGUI/Demo_Table_Element.py

To duplicate the problem with the following steps:
1) Added In the code added window.Element('_table_').SetFocus(force=True) in event=='Double' logic
2) Run Program and click on any table row.
3) Arrow Keys position selection within the table
4) Click on the 'Double' Button
5) SetFocus(force=True) now takes effect
6) Notice: Arrow Keys no longer have effect ..even though PySG shows the table has focus

Note: I've tried dozens of different scenarios -i.e. window['_table_'].Widget.config(takefocus=1) but none take effect. Updating other table values (i.e. num_rows) does not cause the problem

Type of Issues (Enhancement, Error, Bug, Question)

Bug (I think .. or its me)

Operating System

Windows 10

Python version

3.7.2

PySimpleGUI Port and Version

4.3.2 - TK

Your Experience Levels In Months or Years

5yrs: Python programming experience
10+: Programming experience overall
No: Have used another Python GUI Framework (tkiner, Qt, etc) previously (yes/no is fine)?

You have completed these steps:

  • [x] Read instructions on how to file an Issue
  • [x] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [x] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [x] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [x] Note that there are also Demo Programs under each port on GitHub
  • [x] Run your program outside of your debugger (from a command line)
  • [x] Searched through Issues (open and closed) to see if already reported

Code or partial code causing the problem

Partial Code from Demo_Table_Element.py

while True:
    event, values = window.Read()
    print(f"focus:{window.FindElementWithFocus().Key}; event:{event}; values:{values}")
    if event is None:
        break
    if event == 'Double':
        for i in range(len(data)):
            data.append(data[i])
        current_row = values['_table_'][0]
        window.Element('_table_').Update(values=data, select_rows=[current_row])
        window.Element('_table_').SetFocus(force=True)
    elif event == 'Update':
        window.FindElement('_table_').Update( row_colors=((8,'white', 'red'), (9,'black')))

window.Close()
Bug

Most helpful comment

I found a work-around (_mostly from StackOverflow_) for the inactive keyboard issue after a table update. After a table data change/update .. I inserted the following code:

    # Re-Grab table focus using ttk
    window['_TABLE_'].SetFocus(force=True)
    table_row = window['_TABLE_'].Widget.get_children()[current_row]
    window['_TABLE_'].Widget.selection_set(table_row)  # move selection
    window['_TABLE_'].Widget.focus(table_row)  # move focus
    window['_TABLE_'].Widget.see(table_row)  # scroll to show it

`
Now, after I update a value in the table row .. keyboard acts as it should.

Still doing some testing, does this make sense?

All 8 comments

Thank you for the detailed instructions!!! It'll make looking at this much easier. It's really appreciated the effort you took in trying to debug it. Thank you.

Some follow up notes that might help ..
Turning on 'enable_events' in sg.Table did not seem to have any effect as when the arrow keys went silent, no table events were generated as well (_unless I clicked on the table with a mouse_). I also tried a slew of tkinter focus commands to no avail (_albeit, I am by no means a tkinter expert_).

However, I was able to create a work-around by turning on 'return_keyboard_events' in the sg.Window command. So even though the table wasn't generating any keyboard events, the window did generate keyboard events. Bear in mind, I needed to add a bunch of ugly code to highlight and track the selected row.

More testing results. I was running Python 3.7.2 so I converted my test case over to repl.it (running Python 3.6). Still same problem. My repl.it program is: My_Demo_Table_Element2. This sure smells like another Tkinter opportunity :~[

As mentioned above .. once you add a row to the table, the arrow keys have no effect even though the table has focus.

I have no Tkinter experience .. but I'm hesitant to dive in and spend that time to come up to a dead end anyway. I'll be patient and wait .. your knowledge here is much appreciated.

Damn... I'm sorry this is such a pain in the ass. It's awesome to see someone, you're the first, trying repl.it to demonstrate problems, share code.

I was able to use the arrow keys on your repl.it program to move up and down in the table after clicking on Update Data, but I had to click on the table, which is the main problem.

It is a tkinter "problem" of some kind. I'm not sure if there are more forceful ways of giving the table the focus.

I'm painful to read and watch your plight. I don't think the 4.5 release going out tonight, if I ever finish it getting the readme generated, will help any

It surely is a bug somewhere .. but not one that reaches your highest priority. I'll be patient as ever as I did a hard code work-around. Worse case, in the future, I might have to dabble in the QT world. Thank you for that quick response turn around/

Thank you for that! I was feeling really bad about this. I appreciate you doing all this work to try to figure it out. This repl.it thing is actually AMAZING to be able to use. Within seconds I was running your code and seeing exactly what you described, with the code visible and easy to look at.

You've done a LOT on this already. Wow. I'm damned glad I opened up the access to the underlying Widgets and framework. It's really paid off for everyone. Experienced users can go run with stuff without PySimpleGUI getting too much in the way.

I was wondering if you have had any time to work this Table vs. SetFocus vs. Keyboard problem. Perhaps you can give me a work-around where I can force a set focus with tkinter so the keyboard behaves as expected?

I found a work-around (_mostly from StackOverflow_) for the inactive keyboard issue after a table update. After a table data change/update .. I inserted the following code:

    # Re-Grab table focus using ttk
    window['_TABLE_'].SetFocus(force=True)
    table_row = window['_TABLE_'].Widget.get_children()[current_row]
    window['_TABLE_'].Widget.selection_set(table_row)  # move selection
    window['_TABLE_'].Widget.focus(table_row)  # move focus
    window['_TABLE_'].Widget.see(table_row)  # scroll to show it

`
Now, after I update a value in the table row .. keyboard acts as it should.

Still doing some testing, does this make sense?

Was this page helpful?
0 / 5 - 0 ratings