Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing python tkinter output

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

Before I rewrite my program to create a more printer-friendly layout, can someone suggest a way to approach this problem? All of the programming books I have ever read approach the problem of sending output to a printer with a bit of hand-waving, something along the lines of: "It's a difficult problem that depends on interacting with the operating system." I also have a hard time finding resources about this because of all the pages related to printing to the screen.

I am using Python 2.6, on Ubuntu 9.04.

like image 552
Eric Avatar asked Nov 14 '22 10:11

Eric


1 Answers

Turns out that you have to update the canvas before exporting the postscript. Like so:

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.

like image 123
stanga Avatar answered Dec 16 '22 10:12

stanga