8000 COMPAT: use tkagg backend on PyPy by mattip · Pull Request #9356 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

COMPAT: use tkagg backend on PyPy #9356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 12, 2018
Next Next commit
simple plot works
  • Loading branch information
mattip committed Nov 5, 2017
commit b4e2b8aacb626986a1f0f1bf887e1b615a84f204
61 changes: 61 additions & 0 deletions lib/matplotlib/backends/_tkagg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from _tkinter.tklib_cffi import ffi as tkffi, lib as tklib
from _tkinter.tclobj import FromTclStringNDArray
import numpy as np

app = None

def PyAggImagePhoto(photoimage, data, mode):
interp = PyAggImagePhoto.interp
if not tklib.Tk_MainWindow(interp):
raise _tkinter.TclError("this isn't a Tk application")
photo = tklib.Tk_FindPhoto(interp, photoimage)
if not photo:
tklib.Tcl_AppendResult(interp, "destination photo must exist", 0)
return tklib.TCL_ERROR
img = FromTclStringNDArray(data)
has_bbox = False
pBlock = tkffi.new('Tk_PhotoImageBlock[1]')
block = pBlock[0]
block.pixelSize = 1
if mode == 0:
block.offset[0] = block.offset[1] = block.offset[2] = 0
nval = 1
else:
block.offset[0] = 0
block.offset[1] = 1
block.offset[2] = 2
if mode == 1:
block.offset[3] = 0
block.pixelSize = 3
nval = 3
else:
block.offset[3] = 3
block.pixelSize = 4
if has_bbox:
block.width = destwidth
block.height = destheight
block.pitch = deststride
block.pixelPtr = destbuffer

tklib.Tk_PhotoPutBlock_NoComposite(photo, pBlock, destx, desty,
destwidth, destheight);

else:
block.width = shape[1]
block.height = shape[0]
block.pitch = img.strides[0]
block.pixelPtr = tkffi.from_buffer(img)

#/* Clear current contents */
tklib.Tk_PhotoBlank(photo);
#/* Copy opaque block to photo image, and leave the rest to TK */
tklib.Tk_PhotoPutBlock_NoComposite(photo, pBlock, 0, 0,
block.width, block.height);
return tklib.TCL_OK

def tkinit(tk):
tk.createcommand(b"PyAggImagePhoto", PyAggImagePhoto)
PyAggImagePhoto.interp = tk.interp

62 changes: 42 additions & 20 deletions lib/matplotlib/backends/tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,51 @@
import numpy as np

from matplotlib.backends import _tkagg
from platform import python_implementation

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

if bbox is not None:
bbox_array = bbox.__array__()
else:
bbox_array = None
data = np.asarray(aggimage)
try:
tk.call(
"PyAggImagePhoto", photoimage,
id(data), colormode, id(bbox_array))
except Tk.TclError:
if python_implementation() == 'PyPy':
def blit(photoimage, aggimage, bbox=None, colormode=1):
tk = photoimage.tk

if bbox is not None:
bbox_array = bbox.__array__()
else:
bbox_array = None
data = np.asarray(aggimage)
try:
tk.call(
"PyAggImagePhoto", photoimage,
data, colormode, bbox_array)
except Tk.TclError:
_tkagg.tkinit(tk)
tk.call("PyAggImagePhoto", photoimage,
data, colormode, bbox_array)

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

if bbox is not None:
bbox_array = bbox.__array__()
else:
bbox_array = None
data = np.asarray(aggimage)
try:
import pdb;pdb.set_trace()
tk.call(
"PyAggImagePhoto", photoimage,
id(data), colormode, id(bbox_array))
except Tk.TclError:
try:
_tkagg.tkinit(tk.interpaddr(), 1)
except AttributeError:
_tkagg.tkinit(id(tk), 0)
tk.call("PyAggImagePhoto", photoimage,
id(data), colormode, id(bbox_array))
except (ImportError, AttributeError, Tk.TclError):
raise
import pdb;pdb.set_trace()
try:
_tkagg.tkinit(tk.interpaddr(), 1)
except AttributeError:
_tkagg.tkinit(id(tk), 0)
tk.call("PyAggImagePhoto", photoimage,
id(data), colormode, id(bbox_array))
except (ImportError, AttributeError, Tk.TclError):
raise

def test(aggimage):
import time
Expand Down
0