An Introduction to QuickDraw with Python

Jeffrey E. Boyd
September 2008

The following is a summary of a short lecture on how to use QuickDraw on Unix systems (Linux, Mac OS X). Any assistance this may provide with Microsoft Windows is purely by accident, but if it helps, that is great. The focus here is on getting Python to use QuickDraw for graphical display. If you need details on the QuickDraw commands, you should see the documentatin on the QuickDraw site.
  1. Essential Concepts
    1. Unix systems define standard input (aka stdin) and standard output (aka stdout) streams for programs.

      When you use input() or raw_input() to get input in Python, Python gets the input from stdin.

      When you use a print statement in Python, Python sends the output to stdout.

      Under normal circumstances, stdin and stdout come from and go to the terminal console where the Python interpreter is running.

    2. Unix allows you to redirect stdin and stdout on the command line.

      For example, when you run the Python script hello.py as follows

      $ python hello.py > output.txt

      the operating system redirects the standard output from Python to the file output.txt.

      When you run the Python script input.py as follows (with test.txt)

      $ python input.py < test.txt

      the operating system redirects the file test.txt into the standard input of Python.

    3. You can use a pipe to move the stdout from one program into the stdin of another program.

      For example

      $ python hello.py | python input.py

      takes the standard output from the hello.py script and feeds in into the input.py script. Here, the operating system is running two separate processes for two instances of the Python interpreter, one sending its output to the other.

      This ability to use standard output as input to another program is what makes QuickDraw so easy to use for new programmers.

    4. QuickDraw is a Java program that interprets graphics commands that it reads through standard input.

      Download quickdraw.jar from the QuickDraw site and run it as follows.

      java -jar quickdraw.jar

      Here are some example commands that you can try

      circle 100 100 50
      rect 300 300 200 150
      color 255 0 0
      rect 300 200 300 150
      quit

      Observe what these do, then check the QuickDraw documentation to see the extensive set of commands available.

    5. Now you can use Python to generate graphics commands that QuickDraw will execute. For example, run the Python script qd1.py.

      $ python qd1.py

      You should see a set of QuickDraw commands printed to standard output. You can feed these commands to QuickDraw with the following.

      $ python qd1.py | java -jar quickdraw.jar

      You should now see a circle and two rectangles displayed on the output. Python will stop executing but QuickDraw will continue, waiting for input that will never arrive. Terminate QuickDraw by closing its window, or with a Ctrl-C from the console.

    6. Take advantage of expressions, variables, and statements in Python to compose more elaborate graphical programs. For example, qd2.py produces the same output as qd1.py, but makes use of varialbles and expressions to demonstrate how these can be used to generate graphics commands.

    Last modified: 16-Sep-08