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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With