コード例 #1
0
    def new_figure_manager_given_figure(cls, num, figure):
        """
        Create a new figure manager instance for the given figure.
        """
        with _restore_foreground_window_at_end():
            if cbook._get_running_interactive_framework() is None:
                cbook._setup_new_guiapp()
            window = tk.Tk(className="matplotlib")
            window.withdraw()

            # Put a Matplotlib icon on the window rather than the default tk
            # icon.  Tkinter doesn't allow colour icons on linux systems, but
            # tk>=8.5 has a iconphoto command which we call directly.  See
            # http://mail.python.org/pipermail/tkinter-discuss/2006-November/000954.html
            icon_fname = str(cbook._get_data_path(
                'images/matplotlib_128.ppm'))
            icon_img = tk.PhotoImage(file=icon_fname, master=window)
            try:
                window.iconphoto(False, icon_img)
            except Exception as exc:
                # log the failure (due e.g. to Tk version), but carry on
                _log.info('Could not load matplotlib icon: %s', exc)

            canvas = cls.FigureCanvas(figure, master=window)
            manager = cls.FigureManager(canvas, num, window)
            if mpl.is_interactive():
                manager.show()
                canvas.draw_idle()
            return manager
コード例 #2
0
    def get_mpl_interactive_backend(self):
        """
        Get current Matplotlib interactive backend.

        This is different from the current backend because, for instance, the
        user can set first the Qt5 backend, then the Inline one. In that case,
        the current backend is Inline, but the current interactive one is Qt5,
        and this backend can't be changed without a kernel restart.
        """
        # Mapping from frameworks to backend names.
        mapping = {
            'qt': 'QtAgg',  # For Matplotlib 3.5+
            'qt5': 'Qt5Agg',
            'tk': 'TkAgg',
            'macosx': 'MacOSX'
        }

        try:
            # --- Get interactive framework
            framework = None

            # This is necessary because _get_running_interactive_framework
            # can't detect Tk in a Jupyter kernel.
            if hasattr(self, 'app_wrapper'):
                if hasattr(self.app_wrapper, 'app'):
                    import tkinter
                    if isinstance(self.app_wrapper.app, tkinter.Tk):
                        framework = 'tk'

            if framework is None:
                try:
                    # This is necessary for Matplotlib 3.3.0+
                    from matplotlib import cbook
                    framework = cbook._get_running_interactive_framework()
                except AttributeError:
                    # For older versions
                    from matplotlib import backends
                    framework = backends._get_running_interactive_framework()

            # --- Return backend according to framework
            if framework is None:
                # Since no interactive backend has been set yet, this is
                # equivalent to having the inline one.
                return 0
            elif framework in mapping:
                return MPL_BACKENDS_TO_SPYDER[mapping[framework]]
            else:
                # This covers the case of other backends (e.g. Wx or Gtk)
                # which users can set interactively with the %matplotlib
                # magic but not through our Preferences.
                return -1
        except Exception:
            return None
コード例 #3
0
    def new_figure_manager_given_figure(cls, num, figure):
        """
        Create a new figure manager instance for the given figure.
        """
        with _restore_foreground_window_at_end():
            if cbook._get_running_interactive_framework() is None:
                cbook._setup_new_guiapp()
                _c_internal_utils.Win32_SetProcessDpiAwareness_max()
            window = tk.Tk(className="matplotlib")
            window.withdraw()

            # Put a Matplotlib icon on the window rather than the default tk
            # icon. See https://www.tcl.tk/man/tcl/TkCmd/wm.html#M50
            #
            # `ImageTk` can be replaced with `tk` whenever the minimum
            # supported Tk version is increased to 8.6, as Tk 8.6+ natively
            # supports PNG images.
            icon_fname = str(cbook._get_data_path(
                'images/matplotlib.png'))
            icon_img = ImageTk.PhotoImage(file=icon_fname, master=window)

            icon_fname_large = str(cbook._get_data_path(
                'images/matplotlib_large.png'))
            icon_img_large = ImageTk.PhotoImage(
                file=icon_fname_large, master=window)
            try:
                window.iconphoto(False, icon_img_large, icon_img)
            except Exception as exc:
                # log the failure (due e.g. to Tk version), but carry on
                _log.info('Could not load matplotlib icon: %s', exc)

            canvas = cls.FigureCanvas(figure, master=window)
            manager = cls.FigureManager(canvas, num, window)
            if mpl.is_interactive():
                manager.show()
                canvas.draw_idle()
            return manager
コード例 #4
0
 def target():
     while not 'tk' == _get_running_interactive_framework():
         time.sleep(.01)
     plt.close()
     if show_finished_event.wait():
         print('success')