pyx-project / pyx

Repository of PyX, a Python package for the creation of PostScript, PDF, and SVG files.
https://pyx-project.org/
GNU General Public License v2.0
109 stars 18 forks source link

Can't use clip with canvas #20

Closed djnugent closed 4 years ago

djnugent commented 4 years ago

When I run

from pyx import canvas
can = canvas.canvas([canvas.clip])

I get this error

Traceback (most recent call last):
    can = canvas.canvas([canvas.clip])
  File "/usr/local/lib/python3.6/dist-packages/pyx/canvas.py", line 135, in __init__
    attr.checkattrs(attrs, [trafo.trafo_pt, clip, style.style])
  File "/usr/local/lib/python3.6/dist-packages/pyx/attr.py", line 56, in checkattrs
    raise TypeError("instance %r not allowed" % attrs[len(getattrs(attrs, allowedclasses))])
TypeError: instance <class 'pyx.canvas.clip'> not allowed

Pyx version 0.15 Python 3.6

djnugent commented 4 years ago

Ah. This is likely because it is an uninitialized clip object. I misunderstood the purpose of clip. Not a lot of documentation on what clip actually does other than "it clips". Will need to do more digging

djnugent commented 4 years ago

Yep that was it. Pasting solution for anyone who gets stuck on this.

clip = canvas.clip(path.rect(0, 0,10,10)) # clip takes in a path
can = canvas.canvas([clip])
gertingold commented 4 years ago

In such a case, it may be useful to take a look at the example section: https://pyx-project.org/examples/drawing2/clipping.html . I admit that each time I use clipping, which is not very often, I have to look up its usage and that is the place I usually turn to.