Python - Tkinter saved PS images - can't open them Python - Tkinter saved PS images - can't open them tkinter tkinter

Python - Tkinter saved PS images - can't open them


You could try and hand it off to ghostscript in a subprocess. That can render PostScript in almost any bitmap format there is.


I think what may be happening is that the actual frame isn't getting updated in the script. I can reproduce your symptoms -- file produced but payload-free -- by commenting out the cv.update() call in the following:

import Tkinter as tkroot = tk.Tk()root.title("Simple plot")cv = tk.Canvas(width=200, height=200, bg='white')cv.pack()cv.create_text(100, 100, text="hello world!")cv.update() # comment out to make empty postscript!cv.postscript(file="my_drawing.ps", colormode='color')


It turns out the update() function is required prior to postscript call, as specified by tk canvas manpage.

pathName postscript ?option value option value ...?

Note: by default Postscript is only generated for information that appears in the canvas's window on the screen. If the canvas is freshly created it may still have its initial size of 1x1 pixel so nothing will appear in the Postscript. To get around this problem either invoke the "update" command to wait for the canvas window to reach its final size, or else use the -width and -height options to specify the area of the canvas to print.

I have marked this as the fix because the PS file is the preferred save format, and is of much higher quality than the screengrab method.

The working code now becomes:

def drawCircles(MasterList,buildlist):master = Tk()w = Canvas(master, width=1000, height=1000)w.config(bg='white')coordsMain = MasterList[6:]textMain = MasterList[0:2]w.pack()w.create_oval(*coordsMain, width=3, fill = "ivory3")masterLabel = "Source PUID\n" + str(MasterList[3]) + "\nFiles = " + str(MasterList[4])w.create_text(*textMain, text=masterLabel, justify = "center", font=("Helvetica", 16))for i in buildlist: coordsSet = i[6:10] w.create_oval(*coordsSet, width=3, fill = i[5]) set_label = i[3] + "\n" + str(i[4]) + "%" l=w.create_text(4,4, text=set_label, justify = "center", fill="white", font=("Helvetica", 16)) a,b,c,d= (w.bbox(l)) bboxArea =(c-a)*(d-b)    a,b,c,d = i[6:10] circleArea = (c-a)*(d-b) if bboxArea>circleArea:  textSet = i[10:]  j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))  r=w.create_rectangle(w.bbox(j),fill="white", width=0) else:  textSet = i[:2]  j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))  r=w.create_rectangle(w.bbox(j),fill=i[5], width=0)  w.tag_lower(r,j) PUID = str(MasterList[3]) PUID = PUID.replace('/', '-') filename = "\images\\" + PUID + ".PS" w.update()mainloop()