Skip to content

FigureCanvasTkAgg.__init__ steals focus #14081

Open
@loehnertj

Description

@loehnertj

Bug report

Bug summary

I am using embedded plots in a TKinter application. In the FigureCanvasTkAgg.__init__ method, at the end there is a call to focus_set, stealing focus upon construction of the widget. To make matters worse, focus stealing happens asynchronous.

Code for reproduction

    import tkinter as tk
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

    t = tk.Tk()
    b1 = tk.Button(t, text='My Button')
    b1.pack(side='top')
    f = Figure()
    fc = FigureCanvasTkAgg(f, t)
    fc.get_tk_widget().pack()

    # !! after FigureCanvasTkAgg initialization !!
    b1.focus_set()

    t.after(500, lambda: print(t.focus_get()))
    t.mainloop()

Actual outcome

C:\TEMP>python tkagg.py
.!canvas

I.e. canvas has focus although button should have it.

Expected outcome

  1. Creating a widget of any kind should never steal focus.
  2. Using focus_set() afterwards should be able to reset focus.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.0.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.6.2
  • Jupyter version (if applicable):
  • Other libraries:

Python installed via official installer, matplotlib via pip.

The call to focus_set is still present in the master version:

self._tkcanvas.focus_set()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions