'How Can I Print the Contents of my Tkinter Window to a Printer

I made an App to process and display some data in a Tkinter window. I now want to send the contents of the window to a printer for printing on actual paper. However I don't really see any libraries in tkinter or Python to do this. I have to confess I am very new to tkinter and Python.....

Can anyone point me in the right direction?

Thanks.



Solution 1:[1]

tkinter is for a graphics user interface and so is all about display. To print data in a tkinter widget you'd have to retrieve that data depending on what the widget is and put it on the clipboard (can do this in tkinter) or file it (in a separate function) and use a separate printing app to print.

(edited as per comment)

This code does much the same as the subprocess module in a minimalist fashion since you have a specific task on Windows. To test it needs a pdf filepath inserted so I've put in an example that brings up notepad just so it can run as it is.

This has an alternative to checking for print status by use of a manual pause. Then Adobe (or any executable that has the appropriate print facility) can be closed automatically. I'd expect the manual pause to be replaced by an automatic timer set for an estimate time for a document to print. Probably need to consider only the time needed to transfer the document into the printing buffer - but that is up to how you want to operate with your system.


"""Process to start external executable.

   . default is to let process run until parent pause is complete then process is terminated in parent,
     .. this allows time for a process started im the child like printing to finish.
   . option to wait until process has finished before returning to parent.

(proc.py)
"""
import _winapi as win
from os import waitpid
from sys import exc_info as ei

def startproc(exe,cmd, wait=False):
    try:
        ph, th, pid, tid = win.CreateProcess(exe,cmd,None,None,1,0,None,None,None)
        win.CloseHandle(th)
    except:
        print(ei()[1])
        ph = 0
        return (ph,'error')
    if ph > 0:
        if not wait: 
            return (ph,'process still going')
        else:
            pid, exitstatus = waitpid(ph,0)
            return (0,'process done')

#exe = "C:\\Program Files\\Adobe\\Acrobat DC\\Acrobat\\Acrobat.exe"
#cmd = "open <pdf filepath>" 
 
exe = "C:\Windows\System32\\notepad.exe"
cmd = None 

print(__doc__)

proc,msg = startproc(exe,cmd)
print(msg)
if 'done' not in msg:           # manual pause for printing
    input('\n-- carry on --\n') # could be automatic timer
                                           
if msg != 'error' and proc != 0:
    if win.GetExitCodeProcess(proc) == win.STILL_ACTIVE:
        win.TerminateProcess(proc,0)
if 'done' not in msg: print('process closed')

#can delete pdf here

input('\n--- finish ---\n')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1