1

So i have this simple turtle spiral-maker and i was wondering if there is a way to physically print off a copy of my designs that are created by the program.

Code:

import turtle
x= float(input("Angle: "))
y= float(input("Step: "))
scale = int(input("Scale: "))
window = turtle.Screen()
window.bgcolor("white")
turtle = turtle.Turtle()
turtle.color("black")
turtle.speed(100)
turtle.pendown()
size=0
for i in range(scale):
   size+=y
   turtle.left(x)
   turtle.forward(size)

For example, if the variables are:

x=121
y=1
scale=300

The program makes this: Image

Is it possible to print these windows, without the user manually doing so?

Tom Cupis
  • 316
  • 3
  • 24

1 Answers1

3

You can save a screenshot of the turtle Tk canvas as a postscript file like this:

canvas = window.getcanvas()
canvas.postscript(file='/tmp/out.ps')

You could then use subprocess to call a print command. On unix, for example,

import subprocess
subprocess.call(['lpr', '/tmp/out.ps'])

import turtle
import random
import subprocess

x = float(input("Angle: "))
y = float(input("Step: "))
scale = int(input("Scale: "))
# x, y, scale = 121, 1, 300
window = turtle.Screen()
window.bgcolor("white")
t = turtle.Turtle()
t.color("black")
t.speed(100)
t.pendown()
size = 0
for i in range(scale):
    size += y
    t.left(x)
    t.forward(size)

canvas = window.getcanvas()
filename = '/tmp/out.ps'
canvas.postscript(file=filename)
subprocess.call(['lpr', filename])
turtle.mainloop()

Tip: It is better not to reassign module names to other values, as in

turtle = turtle.Turtle()

It makes it hard to call any other function from the original turtle module. Better to give the Turtle instance a new variable name, such as t:

t = turtle.Turtle()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • I get this error: Traceback (most recent call last): File "T:\Desktop\Turtle.py", line 24, in subprocess.call(['lpr', filename]) File "C:\Python34\lib\subprocess.py", line 537, in call with Popen(*popenargs, **kwargs) as p: File "C:\Python34\lib\subprocess.py", line 859, in __init__ restore_signals, start_new_session) File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified – Tom Cupis Feb 20 '16 at 18:10
  • The error is saying `filename` could not by found. Are you specifying a full absolute path? – unutbu Feb 20 '16 at 18:27
  • Yep still not working even if I specify the full path – Tom Cupis Feb 21 '16 at 13:22
  • Was the postscript file successfully saved to disk? Can you view the file? Can you print it from the Windows console with `lpr /path/to/file.ps`? Knowing if any of those steps are going wrong might help us figure out what is going wrong in the Python script. – unutbu Feb 21 '16 at 13:53
  • Yes the file exists but it will not print the canvas – Tom Cupis Feb 21 '16 at 13:55
  • You mean, from the Windows console, `lpr /path/to/file.ps` does not print the image? – unutbu Feb 21 '16 at 13:56
  • Yes basically im pretty sure that the program creates the file but when it try to print it i don't think it can because i think it is trying to open the .ps file and i don't have a program installed to open .ps files – Tom Cupis Feb 21 '16 at 14:01
  • I don't know much about Windows. You might want to [search SuperUser](http://superuser.com/search?q=[windows]+print+command-line) for how to print postscript from the command-line. – unutbu Feb 21 '16 at 14:09
  • Its really strange i tried to print using `os.startfile(filename, "print")` but that failed too! – Tom Cupis Feb 21 '16 at 14:12
  • [This post](http://stackoverflow.com/a/1464974/190597) mentions [Ghostscript for Windows](http://pages.cs.wisc.edu/~ghost/) which has [gsprint](http://pages.cs.wisc.edu/~ghost/gsview/index.htm) which appears to be able to [print postscript to windows printers](http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm). – unutbu Feb 21 '16 at 14:29