Print python tkinter output

I am trying to print the contents of a python tkinter canvas. I tried using the postscript canvas method to create a postscript file, but I get a blank page. I know that this is because I have built-in widgets and they do not get rendered using postscript method.

Before rewriting my program to create a more print-friendly layout, can anyone suggest a way to approach this issue? All the programming books I've ever read relate to the problem of sending output to a printer with a small hand span, something like: "This is a complex problem that depends on interaction with the operating system." It’s also hard for me to find resources about this because of all the pages related to printing on the screen.

I am using Python 2.6 on Ubuntu 9.04.

+5
source share
3 answers

Turns out you need to update the canvas before exporting postscript. For instance:

from Tkinter import * 

root = Tk() 
canvas = Canvas(bg='white', width = 200, height = 200) 
canvas.pack() 

canvas.create_line(0, 0, 199, 199, fill="blue", width = 5) 
canvas.create_line(0, 199, 199, 0, fill="blue", width = 5) 

canvas.update() 
canvas.postscript(file = "x.ps") 

root.mainloop() 

Thanks to Rio here for the solution.

+1
source

Well, this is a complex problem that depends on interactions with the operating system. (Sorry, could not resist!)

The canvas-to-postscript solution only works for things drawn on canvas - it does not handle embedded windows. There are libraries that can convert the canvas to PDF, but I have no experience with them, and I don’t know if they handle embedded windows or not (I guess not).

pdflib, C, python tcl (, , Tkinter). , , , , . , - , , ..

Tk; , , , , , -, - .

0

, Tkinter. , , PIL, API.

, , ImageGrab PIL.

wxPython - . Qt, , , . Graphics View.

0
source

All Articles