tkintermapview PhotoImage object has no attribute '_PhotoImage__photo'

230 Views Asked by At

I am making a project based on tkintermapview, but it throws the error when the following code is run.

import tkintermapview as tkmap


        self.map = tkmap.TkinterMapView(self.__map_frame, width=self.__map_width,
                                        height=self.__height, corner_radius=0)
        # google normal tile server
        self.map.set_tile_server("https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22)
        # google satellite tile server
        # self.map.set_tile_server("https://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22)

        # self.map.set_tile_server("http://c.tile.stamen.com/watercolor/{z}/{x}/{y}.png")  # painting style

        self.map.pack(fill=tk.BOTH)
        self.map.set_address("kathmandu")

if the last line i.e. set_address() is removed then it runs fine otherwise it throws the error.

following is the error message:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Exception ignored in: <function PhotoImage.__del__ at 0x7fa9e10ed510>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

Process finished with exit code 0

I tried the following code as well to reproduce the error

from tkintermapview import TkinterMapView


root_tk = tkinter.Tk()
root_tk.geometry(f"{600}x{400}")
root_tk.title("map_view_simple_example.py")

# create map widget
map_widget = TkinterMapView(root_tk, width=600, height=400, corner_radius=0)
map_widget.pack(fill="both", expand=True)

# google normal tile server
map_widget.set_tile_server("https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22)

map_widget.set_address("chyasal")

root_tk.mainloop()

But this time the error is shown for the first time only. After that the program works correctly.

But again when I change the place to new location say set_address("Manang") again for the first launch, the same error is occured. But this is not the case with all the places, I tried many different places inside the set_address() method, but only some of them caused the error.

I have one more question
My project should enable a user to pick the pick up and drop off location on the map and the map should calculate the shortest road and its distance(length of road) between the two locations.
Is tkintermapview good choice or is there a better way to display google map and implement this requirement in tkinter...?

1

There are 1 best solutions below

0
rd_nielsen On

I have seen the same error when using tkintermapview, and the flood of messages to stderr is inconsequential but ugly. My solution was to monkeypatch the PhotoImage.del method as follows:

from PIL import ImageTk
def new_img_del(img_self):
    try:
        name = img_self.__photo.name
        img_self.__photo.name = None
        img_self.__photo.tk.call("image", "delete", name)
    except Exception:
        pass
ImageTk.PhotoImage.__del__ = new_img_del

This worked for my application, but yours may be different.