0
canvas_width = 512
canvas_height = 512
root = Tk()
canvas = Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()
canvas.create_rectangle(0, 0, canvas_width, canvas_height / 2, fill='green')
ps = canvas.postscript(colormode='color')
img = PIL.Image.open(io.BytesIO(ps.encode('utf-8')))
img.save("/home/test.png")

If you execute this code, you will see that the saved file, test.png, contains only one black pixel. Instead, it should contain 512x256 green pixels and 512x256 undefined-color pixels.

Do you know why?

JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70

1 Answers1

1

You'd have to redraw the canvas events by using canvas.update() or canvas.update_idletasks() before capturing. The difference is that update() is processing user events while update_idletasks() is just forcefully redrawing the window. Only then you'd be able to capture a postscript since all the text and graphics will already be there to capture and convert it to an image. Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24