[Tutor] How to print ALL contents of a scrolled Tkinter window?

Alan Gauld alan.gauld at btinternet.com
Fri Apr 17 21:28:21 CEST 2015


On 17/04/15 14:26, boB Stepp wrote:
> Solaris 10, Python 2.4.4
>
> Thanks to earlier help from this list, I can now print a particular
> Tkinter-generated window. But this will only print what is currently
> viewable on the screen.

Because it is effectively taking a screen shot.

> In the case of scrolled information that is
> currently outside the viewing area, it would be missed by such a print

That's why GUI printing generally uses an entirely different
technique to print things (see my earlier email). In essence
this requires you to separate the data content and format
from the GUI presentation. The printed presentation must be
prepared by you the programmer and output to the new display
context(a printer). This means you need to decide how to
handle page breaks, page width and orientation etc etc.

Its a whole different ballgame and much more complex than
simply issuing a print command. Hard copy output from a
GUI is one of the hardest things to do in almost any GUI,
especially if you want it truly WYSIWYG.

My personal favourite approach is to abandon WYSIWYG and
go for WYSRWYG - What you see resembles what you'll get...
And then I reformat the data using HTML, send it to a file
and print that file using whatever HTML rendering engine
I can find.

wxPython makes printing easier but even there its a
whole chapter(ch17) of the book and its at the end so
assumes you already know all the background around sizers,
device context and the like.... The example program - for
printing a formatted text document - runs to 2.5 pages of
code. And that's the easiest printing framework I've seen.

> are along the lines of: 1) Determine how many rows of information
> appear in the viewing area. 2) Determine how many total rows of
> information exist to be printed. 3) Figure out how to programmatically
> do a *manual* scroll to bring up the hidden scrolled information. 4)
> Repeatedly apply the printing method. While I can probably make this
> approach work (It *seems* conceptually simple.), I cannot help but
> feel there is a much better way...

Its never going to be pretty but multiple screen captures
can work. Its certainly easy enough to reposition a scroll
bar programmatically from within a while loop. Figuring out
how many lines will depend on all sorts of things like
the size of fonts being used, how widgets are laid out.
That's what gets tricky, especially if users don't have
standardised screen sizes etc.

> I intend to scour my available Tkinter documentation to see if there
> are root window level and scrolled area commands that might suggest
> another approach.

Not really. Tkinter (and Tk) basically sidesteps printing
to hard copy. There simply is no built in support. You have
to format it yourself.

Check out IDLE - even it doesn't have any built in
formatted print. It only has 'Print Window', which is
just plain text.
Although even looking at how they did that might
help too. Remember you have the code to IDLE and
its built using Tkinter...

-- 
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