aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/matplotlib/py2/matplotlib/backends/tkagg.py
blob: 072fcb48fee654746f96a3a575424fcbe64b09d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import six
from six.moves import tkinter as Tk

import numpy as np

from matplotlib.backends import _tkagg

def blit(photoimage, aggimage, bbox=None, colormode=1):
    tk = photoimage.tk

    if bbox is not None:
        bbox_array = bbox.__array__()
        # x1, x2, y1, y2
        bboxptr = (bbox_array[0, 0], bbox_array[1, 0],
                   bbox_array[0, 1], bbox_array[1, 1])
    else:
        bboxptr = 0
    data = np.asarray(aggimage)
    dataptr = (data.shape[0], data.shape[1], data.ctypes.data)
    try:
        tk.call(
            "PyAggImagePhoto", photoimage,
            dataptr, colormode, bboxptr)
    except Tk.TclError:
        if hasattr(tk, 'interpaddr'):
            _tkagg.tkinit(tk.interpaddr(), 1)
        else:
            # very old python?
            _tkagg.tkinit(tk, 0)
        tk.call("PyAggImagePhoto", photoimage,
                dataptr, colormode, bboxptr)

def test(aggimage):
    r = Tk.Tk()
    c = Tk.Canvas(r, width=aggimage.width, height=aggimage.height)
    c.pack()
    p = Tk.PhotoImage(width=aggimage.width, height=aggimage.height)
    blit(p, aggimage)
    c.create_image(aggimage.width,aggimage.height,image=p)
    blit(p, aggimage)
    while True: r.update_idletasks()