pythontkintertkintermapview

Changing colour of Tkintermapview Polygon during run


I am creating a program that requires changing the colour of a polygon using tkintermapview during the program. I have tried the following:

polygon1 = map_widget.set_polygon([(52, -1),
                                   (52.5, -1.1),
                                   (52.2, -1.5)],
                                  fill_color = 'red',
                                  name='Test location')

polygon1.fill_color = 'black'
print(polygon1.fill_color)

output:

'black'

However, the colour on the map does not change from red to black. How would I achieve the desired outcome?


Solution

  • Changing the attribute fill_color will not update the polygon already drawn on the map automatically.

    As the map is drawn in a tkinter Canvas object and the polygon is a polygon item of the canvas object, so you can using Canvas.itemconfig() to change the fill color you want as below:

    polygon1 = map_widget.set_polygon([(52, -1),
                                       (52.5, -1.1),
                                       (52.2, -1.5)],
                                      fill_color = 'red',
                                      name='Test location')
    
    map_widget.canvas.itemconfig(polygon1.canvas_polygon, fill="yellow")
    

    Note that map_widget.canvas is the Canvas object and polygon1.canvas_polygon is the item ID of the polygon item (according to the source code of tkintermapview).

    Result:

    enter image description here