Skip to content

Commit a1d7d6c

Browse files
committed
Set focus properly to ensure mouse events get delivered to Tk canvas
1 parent 04834bc commit a1d7d6c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ def enter_notify_event(self, event):
279279
guiEvent=event, xy=self._event_mpl_coords(event))
280280

281281
def button_press_event(self, event, dblclick=False):
282+
# set focus to the canvas so that it can receive keyboard events
283+
self._tkcanvas.focus_set()
284+
282285
num = getattr(event, 'num', None)
283286
if sys.platform == 'darwin': # 2 and 3 are reversed.
284287
num = {2: 3, 3: 2}.get(num, num)
@@ -467,6 +470,7 @@ def destroy(*args):
467470
Gcf.destroy(self)
468471
self.window.protocol("WM_DELETE_WINDOW", destroy)
469472
self.window.deiconify()
473+
self.canvas._tkcanvas.focus_set()
470474
else:
471475
self.canvas.draw_idle()
472476
if mpl.rcParams['figure.raise_window']:

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,33 @@ class Toolbar(NavigationToolbar2Tk):
186186
print("success")
187187
Toolbar(fig.canvas, fig.canvas.manager.window) # This should not raise.
188188
print("success")
189+
190+
191+
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
192+
@_isolated_tk_test(success_count=1)
193+
def test_canvas_focus(): # pragma: no cover
194+
import tkinter as tk
195+
import matplotlib.pyplot as plt
196+
success = []
197+
198+
def check_focus():
199+
tkcanvas = fig.canvas.get_tk_widget()
200+
# Give the plot window time to appear
201+
if not tkcanvas.winfo_viewable():
202+
tkcanvas.wait_visibility()
203+
# Make sure the canvas has the focus, so that it's able to receive
204+
# keyboard events.
205+
if tkcanvas.focus_lastfor() == tkcanvas:
206+
success.append(True)
207+
plt.close()
208+
root.destroy()
209+
210+
root = tk.Tk()
211+
fig = plt.figure()
212+
plt.plot([1, 2, 3])
213+
root.after(0, plt.show)
214+
root.after(100, check_focus)
215+
root.mainloop()
216+
217+
if success:
218+
print("success")

0 commit comments

Comments
 (0)