[Tkinter-discuss] Generating Postscript from canvas whilst hidden

Matt Keenan matt.keenan at oracle.com
Tue Mar 1 17:05:21 CET 2011


Hi,

Looking for the best means of generating postscript from a canvas widget 
but having the canvas widget in the background, and not visible to the user.

I have an application that I'd like to batch generate a web site from, 
including a set of graphs.

the graphs are plotted on a canvas widget with a convenience function 
and then postscript is generated using the canvas.postscript() method.

The only means I can see of generating valid postscript is when the 
canvas widget and root are displayed to the user. I'd prefer if this 
could be hidden.

Following code is a rough example of what happens when the user clicks 
the generate button

    def generate_webpage(self, emplist):
        myroot = Tk()
        mycanvas = Canvas(myroot, width=700, height=400)
        mycanvas.pack()
        mycanvas.root = myroot

        for i in range(0, len(emplist)):
            # PostScript file name
            psname = "graph%s.ps" % (i)

            # Clear canvas down
            canvas.delete(ALL)

            # Plot graph on canvas
            PlotLineGraph(mycanvas, emplist[i])

            # Update tasks and canvas
            self.update_idletasks()
            mycanvas.update()
            mycanvas.update_idletasks()

            # Generate postscript
            mycanvas.postscript(file=psname, pagewidth=675,
                pageheight=400)

        myroot.destroy()


As can be seen, when generate_webpage is clicked, a new popup window is 
shown and each graph as generated is shown within the window as it's done.

If I add myroot.withdraw() to above method, just after packing mycanvas, 
then no popup appears but all the postsript files are blank
nothing is actually plotted on the canvas !!

Does anyone know of a way to get around this ?

cheers

Matt




More information about the Tkinter-discuss mailing list