Well, few things:
First of all, python has this weird garbage collecting issue with tkinter images, in order to really pass the image to your canvas, you need to anchor down first otherwise it would just got wiped when it was passed to canvas.
This is what i did after learning from other people's example:
window.image = create_image(WIDTH, HEIGHT)
Once you anchor it down to your Tk()
, it shouldn't get collected and erased anymore as long as your Tk()
exists.
Second problem is your placement of image, you might want to place it to the center of your canvas instead of the corner of it:
canvas.create_image(WIDTH//2, HEIGHT//2, image=window.image)
In the end your program would look like this:
window = tk.Tk()
canvas = tk.Canvas(window, width=WIDTH, height=HEIGHT)
window.image = create_image(WIDTH, HEIGHT)
canvas.create_image(WIDTH//2, HEIGHT//2, image=window.image)
canvas.pack(fill=tk.BOTH, expand=tk.TRUE)
window.mainloop()
BTW a circle of a radius of 10 is just too small in a canvas of 640 x 480, you might want to increase this number to 100 or so.
if x**2 + y**2 < 10000:
Like that^