[Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

Alan Gauld alan.gauld at btinternet.com
Wed Apr 15 18:13:40 CEST 2015


On 15/04/15 13:55, boB Stepp wrote:
> Solaris 10, Python 2.4.4
>
> I have very little experience with issuing print commands using a Unix
> environment. Despite this, I wish to design a Tkinter window with a
> "Print" button, which, when clicked, would create a copy of the
> contents of the window as a .pdf file. GhostScript is available on my
> systems.

Your problem is that Tkinter does not really support the concept
of printing to hard copy. Other GUI frameworks (like WxPython)
do this much better. So anything you do will be a bit of a kluge.
There are some valid reasons for this: printing is extremely OS 
dependant. And Tk as a cross OS platform doesn't want to have
to deal with all the complexity(*).

The usual solution is to render the output in some print
friendly file format (html, groff, SVG etc) and then send that
to the appropriate print command. However if you really want
a screen image you might be better invoking one of the many
screen capture programs and then printing its output?

(*)
The main culprits are
- Windows which uses a "Device Context" to render its UI. The device can 
be either a screen or a printer (or notionally anything else) so in 
theory you just swap the DC and send the GUI to the new device. In 
practice its not that easy.
- MacOSX uses a Cocoa framework which, in turn, uses OpenGL primitives 
to render the UI and like Windows can theoretically render the UI on non 
screen devices, It can also render it as PDF natively which is how it 
normally prints screen images.
- *nix usually relies on rendering the output *content* on a new device 
or in a new format. Printing screenshots in generic Unix is a bit of an 
afterthought IMHO...Possibly a consequence of X having been designed 
before the days of affordable colour printers - the same reason Steve 
Jobs didn't bother with colour on the early Macs - if you couldn't print 
it, there was no point, he thought...

Printing UI images and WYSIWYG type output is one of the few triggers 
that drive me to WxPython. It handles it much more easily than Tk.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list