Search code examples
pythontkintertkinter-canvas

Filling a Tkinter Canvas. Whats the easiest way to fill figure, which composed with different base shapes and images?


I'm creating a figure from several primitive shapes, what is the easiest way for me to paint them all at the same time?

Its how i create the figure:

from tkinter import *
root=Tk()
c=Canvas(root,width=700,height=500,bg='blue',cursor="pencil")
c.create_arc(300,200,250,175,outline='white',style=ARC,start=0,extent=180)#1
c.create_arc(250,200,200,175,outline='white',style=ARC,start=0,extent=180)#2
c.create_arc(200,200,150,175,outline='white',style=ARC,start=0,extent=180)#3
c.create_arc(150,200,100,175,outline='white',style=ARC,start=0,extent=180)#4
c.create_arc(100,100,300,270,outline='white',style=ARC,start=0,extent=180)#umbrella
c.create_line(200,185,200,300,fill='white')
c.create_arc(200,325,250,275,outline='white',style=ARC,start=180,extent=180)
c.pack()
root.mainloop()

I'm trying to find an analogue of the FloodFill command (from Pascal), but I couldn't

I would be very grateful for your help


Solution

  • You can group canvas objects together using tags. This can be used to collectively change fill, outline and other properties as well as group control of movement and scale.

    NOTE: canvas line only uses fill and not outline so for a single line try using polygon.

    from tkinter import *
    
    root=Tk()
    
    c=Canvas(root,width=700,height=500,bg='blue',cursor="pencil")
    c.pack()
    
    # polygon instead of line
    c.create_polygon(200,185,200,300, tags="hue")
    c.create_arc(200,325,250,275, style=ARC, tags="hue", start=180, extent=180)
    c.create_arc(300,200,250,175, style=ARC, tags = "hue", start=0, extent=180)#1
    c.create_arc(250,200,200,175, style=ARC, tags = "hue", start=0, extent=180)#2
    c.create_arc(200,200,150,175, style=ARC, tags = "hue", start=0, extent=180)#3
    c.create_arc(150,200,100,175, style=ARC, tags = "hue", start=0, extent=180)#4
    c.create_arc(100,100,300,270, style=ARC, tags = "hue", start=0, extent=180)#umbrella
    
    # Color and change properties of all objects in one statement using tags
    c.itemconfig("hue", fill="orange", outline="yellow", width=4)
    
    root.mainloop()