Python Tkinter - save canvas - tkinter crashes Python Tkinter - save canvas - tkinter crashes tkinter tkinter

Python Tkinter - save canvas - tkinter crashes


It seems to work if you simply move the w.postscript() call above the mainloop() call. This means that the error is due to calling w.postscript() after the Tkinter GUI is closed (since mainloop() runs until the window is closed).

I noticed this was a difference between your code and this example, so I tried it and saw that the .ps file was created as soon as the script is ran.


Update for python 3:

Please note that:
- a call to update on the canvas is necessary.
- The Canvas background is not saved.
- Only the visible part of canvas is saved.

import tkinter as tkimport randomCOLORS = ["white", "black", "red", "green", "blue", "cyan", "yellow", "magenta"]root = tk.Tk()cv = Canvas(root, width=1000, height=1000, bg='cyan')cv.pack()for _ in range(1000):    coordinates = [random.randrange(0, 1000) for _ in range(4)]    cv.create_oval(*coordinates, outline=random.choice(COLORS))cv.update()cv.postscript(file="my_drawing.ps")mainloop()

enter image description here