@@ -162,6 +162,7 @@ def _on_timer(self):
162162
163163class FigureCanvasTk (FigureCanvasBase ):
164164 required_interactive_framework = "tk"
165+ manager_class = _api .classproperty (lambda cls : FigureManagerTk )
165166
166167 def __init__ (self , figure = None , master = None ):
167168 super ().__init__ (figure )
@@ -433,6 +434,43 @@ def __init__(self, canvas, num, window):
433434
434435 self ._shown = False
435436
437+ @classmethod
438+ def create_with_canvas (cls , canvas_class , figure , num ):
439+ # docstring inherited
440+ with _restore_foreground_window_at_end ():
441+ if cbook ._get_running_interactive_framework () is None :
442+ cbook ._setup_new_guiapp ()
443+ _c_internal_utils .Win32_SetProcessDpiAwareness_max ()
444+ window = tk .Tk (className = "matplotlib" )
445+ window .withdraw ()
446+
447+ # Put a Matplotlib icon on the window rather than the default tk
448+ # icon. See https://www.tcl.tk/man/tcl/TkCmd/wm.html#M50
449+ #
450+ # `ImageTk` can be replaced with `tk` whenever the minimum
451+ # supported Tk version is increased to 8.6, as Tk 8.6+ natively
452+ # supports PNG images.
453+ icon_fname = str (cbook ._get_data_path (
454+ 'images/matplotlib.png' ))
455+ icon_img = ImageTk .PhotoImage (file = icon_fname , master = window )
456+
457+ icon_fname_large = str (cbook ._get_data_path (
458+ 'images/matplotlib_large.png' ))
459+ icon_img_large = ImageTk .PhotoImage (
460+ file = icon_fname_large , master = window )
461+ try :
462+ window .iconphoto (False , icon_img_large , icon_img )
463+ except Exception as exc :
464+ # log the failure (due e.g. to Tk version), but carry on
465+ _log .info ('Could not load matplotlib icon: %s' , exc )
466+
467+ canvas = canvas_class (figure , master = window )
468+ manager = cls (canvas , num , window )
469+ if mpl .is_interactive ():
470+ manager .show ()
471+ canvas .draw_idle ()
472+ return manager
473+
436474 def _update_window_dpi (self , * args ):
437475 newdpi = self ._window_dpi .get ()
438476 self .window .call ('tk' , 'scaling' , newdpi / 72 )
@@ -966,45 +1004,6 @@ def trigger(self, *args):
9661004class _BackendTk (_Backend ):
9671005 FigureManager = FigureManagerTk
9681006
969- @classmethod
970- def new_figure_manager_given_figure (cls , num , figure ):
971- """
972- Create a new figure manager instance for the given figure.
973- """
974- with _restore_foreground_window_at_end ():
975- if cbook ._get_running_interactive_framework () is None :
976- cbook ._setup_new_guiapp ()
977- _c_internal_utils .Win32_SetProcessDpiAwareness_max ()
978- window = tk .Tk (className = "matplotlib" )
979- window .withdraw ()
980-
981- # Put a Matplotlib icon on the window rather than the default tk
982- # icon. See https://www.tcl.tk/man/tcl/TkCmd/wm.html#M50
983- #
984- # `ImageTk` can be replaced with `tk` whenever the minimum
985- # supported Tk version is increased to 8.6, as Tk 8.6+ natively
986- # supports PNG images.
987- icon_fname = str (cbook ._get_data_path (
988- 'images/matplotlib.png' ))
989- icon_img = ImageTk .PhotoImage (file = icon_fname , master = window )
990-
991- icon_fname_large = str (cbook ._get_data_path (
992- 'images/matplotlib_large.png' ))
993- icon_img_large = ImageTk .PhotoImage (
994- file = icon_fname_large , master = window )
995- try :
996- window .iconphoto (False , icon_img_large , icon_img )
997- except Exception as exc :
998- # log the failure (due e.g. to Tk version), but carry on
999- _log .info ('Could not load matplotlib icon: %s' , exc )
1000-
1001- canvas = cls .FigureCanvas (figure , master = window )
1002- manager = cls .FigureManager (canvas , num , window )
1003- if mpl .is_interactive ():
1004- manager .show ()
1005- canvas .draw_idle ()
1006- return manager
1007-
10081007 @staticmethod
10091008 def mainloop ():
10101009 managers = Gcf .get_all_fig_managers ()
0 commit comments