Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pressing tab breaks keymap in CanvasTk #13739

Closed
wants to merge 1 commit into from

Conversation

LeiSurrre
Copy link

@LeiSurrre LeiSurrre commented Mar 22, 2019

Hi, I'm opening this as part of coursework from University of Toronto Scarborough's CSCD01 taught by Dr. Anya Tafliovich @atafliovich. (https://www.utsc.utoronto.ca/~atafliovich/cscd01/index.html)

PR Summary

Closes #13484 - Matplotlib keymap stop working after pressing tab

Bug Analysis and Technical Commentary:

When using figureCanvas and NavigationToolBar embedded in Tkinter, Matplotlib has some key mappings to NavigationToolBar actions. However, if tab is pressed, none of the key mappings works and clicking in the figure/plot does not help. The methods to bring the key mappings back are to use either alt+tab or pressing tab for a few times.

The problem is with the default Tkinter tab action, it will move the focus around on different objects in the Tinkter canvas, in this case, buttons in the NavigationToolBar. The key mapping functions are set on figureCanvas so they work only when the focus in on the figure.

The fix is to add a tab_event to handle the case where tab is pressed on the Tkinter Canvas. The tab_event will set focus to the figureCanvas and call key_press from FigureCanvasTk, and uses return 'break' to disable the default Tkinter tab action. By binding the Tkinter tab pressed event "<tab>" to my customized tab_event fixes the misbehaviour when tab is pressed. I also added a default tab key recognition to the FigureCanvasTk class since None was returned before when tab is pressed.

Acceptance Test:

  • create FigureCanvasTkAgg and NavigationToolbar2Tk based on a Tkinter root
  • pressing tab does not move the focus and disable any key mappings

Result:

pressing tab behaves as expected

Example code:

see matplotlib embedded in tk example

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak jklymak changed the title add fix to issue 13484, pressing tab breaks keymap in CanvasTk Fix pressing tab breaks keymap in CanvasTk Mar 24, 2019
@jklymak jklymak added this to the v3.1.1 milestone Mar 24, 2019
@timhoffm
Copy link
Member

timhoffm commented Apr 2, 2019

Thanks for your contribution. However, I don't think this is the right way to go. You're overriding the tab functionality and by this break keyboard navigation in the figure.

As stated in #13484 (comment)

I don' think this is a bug, would merge a PR to that example showing how to set up Tk to change it's focus to the plot if you click on that widget.

An alternative solution might be to bind the shortcuts to the window and not to the canvas. - Not sure if this is possible with Tk (with Qt it is) and if that's manageable in the current framework (essentially all actions are bound to the canvas and matplotlib does not know about the window).

@LeiSurrre
Copy link
Author

LeiSurrre commented Apr 3, 2019

Hi, thanks for the reply. I did not think about the tab keyboard navigation was designed to be like that. Now I think the fix in the comment should be the proper fix.

@timhoffm
Copy link
Member

Closing since rebinding <Tab> is not the right solution to the issue; see discussion above.

@LeiSurrre Even though this didn't get merged, thanks a lot for working on the issue and trying to contribute a fix! Maybe see you back some time.

@timhoffm timhoffm closed this Apr 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Matplotlib keymap stop working after pressing tab
3 participants