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

boB Stepp robertvstepp at gmail.com
Sat Apr 18 22:17:42 CEST 2015


On Fri, Apr 17, 2015 at 2:28 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 17/04/15 14:26, boB Stepp wrote:
>>
>> Solaris 10, Python 2.4.4
>>

[...]

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

I had started thinking along these lines, but realized that I possess
a severe lack of knowledge as to the details of the printer does its
*magic*. I used to know a bit about how dot-matrix printers
(non-color) worked, but that has been a very long time ago...

You mentioned groff in the other thread. Is this relatively easy to
adapt to its formatting commands? Is it likely to be on my bare-bones
Solaris 10 workstation? I will have to check to see what is present
Monday.

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

I am surprised that Python does not have a standard library module to
help with this. I see that there are third-party modules, but that
does not help me within my work environment constraints.

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

Are there such HTML rendering engines likely to be present in a
Solaris 10 environment, which is a rather bare-bones environment? I
mean outside of a web browser. I can already use my existing template
to auto-generate the appropriate HTML formatting (Once I write the
needed functions to do so. But it should logically no different than
how I interpret my template to render the Tkinter GUI display.), save
that in a file and then? This would be needed to be done
programmatically, not manually by the user. The desired end result is
a pdf document to be stored electronically for posterity.

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

Which book are you referencing here? Would it be "Wxpython in Action"?

>> 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 may or may not go this way. I already wrote a set of scripts (in
shell and Perl) a few years ago to allow the user to click and drag an
area of their screen to capture into a pdf document. It leverages the
existing printer configurations and tools in the planning software.
When the user is finishing making captures, he clicks the "Print to
pdf" button and it converts the captures into a single pdf document,
which it then ftps to the desired destination folder on a different
portion of our intranet. This would provide a workable solution until
I learn more.

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

I did not find even a hint of anything beyond the already known Canvas
widget postscript capability.

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

I will make time to do this. Hopefully it will provide better modes of
thinking on my part!

Thanks, Alan!

boB


More information about the Tutor mailing list