backend_tkcairo.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import sys
  2. import numpy as np
  3. from . import _backend_tk
  4. from .backend_cairo import cairo, FigureCanvasCairo, RendererCairo
  5. from ._backend_tk import _BackendTk, FigureCanvasTk
  6. class FigureCanvasTkCairo(FigureCanvasCairo, FigureCanvasTk):
  7. def __init__(self, *args, **kwargs):
  8. super(FigureCanvasTkCairo, self).__init__(*args, **kwargs)
  9. self._renderer = RendererCairo(self.figure.dpi)
  10. def draw(self):
  11. width = int(self.figure.bbox.width)
  12. height = int(self.figure.bbox.height)
  13. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
  14. self._renderer.set_ctx_from_surface(surface)
  15. self._renderer.set_width_height(width, height)
  16. self.figure.draw(self._renderer)
  17. buf = np.reshape(surface.get_data(), (height, width, 4))
  18. _backend_tk.blit(
  19. self._tkphoto, buf,
  20. (2, 1, 0, 3) if sys.byteorder == "little" else (1, 2, 3, 0))
  21. self._master.update_idletasks()
  22. @_BackendTk.export
  23. class _BackendTkCairo(_BackendTk):
  24. FigureCanvas = FigureCanvasTkCairo