TkinterMapView icon indicating copy to clipboard operation
TkinterMapView copied to clipboard

Photo Image Error

Open Kromilan opened this issue 3 years ago • 17 comments

Hi. I love this project, but I sometimes receive these errors upon closing a window containing this widget. I experimented a bit, but I couldn't find a pattern for when it happens. It looks like it's trying to update the images and when you close the window right while the update happens, you get this error. But I could be wrong. It was never harmful anyways. It's just a mysterious text in the console, but it causes no problems. Still, I would like to find a way to remove/avoid this error. Can you give me some insight into it?

Exception in thread Thread-1 (pre_cache):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "/home/milan/.local/lib/python3.10/site-packages/tkintermapview/map_widget.py", line 278, in pre_cache
    self.request_image(zoom, x, self.pre_cache_position[1] + radius, db_cursor=db_cursor)
  File "/home/milan/.local/lib/python3.10/site-packages/tkintermapview/map_widget.py", line 348, in request_image
    image_tk = ImageTk.PhotoImage(image)
  File "/usr/lib/python3.10/site-packages/PIL/ImageTk.py", line 112, in __init__
    self.__photo = tkinter.PhotoImage(**kw)
  File "/usr/lib/python3.10/tkinter/__init__.py", line 4093, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.10/tkinter/__init__.py", line 4026, in __init__
    master = _get_default_root('create image')
  File "/usr/lib/python3.10/tkinter/__init__.py", line 297, in _get_default_root
    raise RuntimeError(f"Too early to {what}: no default root window")
RuntimeError: Too early to create image: no default root window
Exception ignored in: <function PhotoImage.__del__ at 0x7fa69afba5f0>
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/PIL/ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

Kromilan avatar Apr 01 '22 09:04 Kromilan

Yeah, it looks like there is a thread running where PhotoImage objects are created even after the window is closed. I will try to catch these errors or stop the thread in time.

TomSchimansky avatar Apr 01 '22 11:04 TomSchimansky

The error should be resolved now with version 1.6, if you experience it again, just mention it here...

TomSchimansky avatar Apr 10 '22 10:04 TomSchimansky

Thank you greatly for looking into it. :)

Kromilan avatar Apr 10 '22 21:04 Kromilan

I have a new problem ! Even if i download version 1.6 i have this f*cking error again... How do i do ? I'm really confuse because i see many people making apps with this and all of their apps works and mine not working for none reasons !

Anyway, good bye ! Miyu.

miyucode avatar May 25 '22 14:05 miyucode

Can you post your code or a smaller example where the error appears?

TomSchimansky avatar May 25 '22 15:05 TomSchimansky

from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
from PIL import *

import tkintermapview
import tkinter
import tkinter.messagebox as mb

def mapsapp():
	# create tkinter window
	root_tk = tkinter.Tk()
	root_tk.geometry(f"{1000}x{700}")
	root_tk.title("map_view_simple_example.py")

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

	# set other tile server (standard is OpenStreetMap)
	# 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)  # google normal
	# map_widget.set_tile_server("https://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22)  # google satellite

	# set current position and zoom
	# map_widget.set_position(52.516268, 13.377695, marker=False)  # Berlin, Germany
	# map_widget.set_zoom(17)

	# set current position with address
	# map_widget.set_address("Berlin Germany", marker=False)

	def marker_click(marker):
	    print(f"marker clicked - text: {marker.text}  position: {marker.position}")

	# set a position marker (also with a custom color and command on click)
	marker_2 = map_widget.set_marker(52.516268, 13.377695, text="Brandenburger Tor", command=marker_click)
	marker_3 = map_widget.set_marker(52.55, 13.4, text="52.55, 13.4")
	# marker_3.set_position(...)
	# marker_3.set_text(...)
	# marker_3.delete()

	# set a path
	path_1 = map_widget.set_path([marker_2.position, marker_3.position, (52.568, 13.4), (52.569, 13.35)])
	# path_1.add_position(...)
	# path_1.remove_position(...)
	# path_1.delete()

	root_tk.mainloop()

This is my code ! My error is the same that Kromilan !

miyucode avatar May 25 '22 15:05 miyucode

Which operating system are you working on?

TomSchimansky avatar May 25 '22 15:05 TomSchimansky

I'm working on Windows 10 !

miyucode avatar May 25 '22 15:05 miyucode

I have tkintermapview on an install on my work laptop and it seems to run fine. However, when I try to get it to work on my private laptop, it keeps throwing this error:

File "***\anaconda3\lib\site-packages\PIL\ImageTk.py", line 118, in del name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

EDIT: it seems this error pops up when the .mainloop() command is not active (it was for some reason I can't remember commented in my code)

mpageers avatar Jun 20 '22 07:06 mpageers

Same problem in raspberrypi os. It's because lack of Pillow module. Installing pillow >9.0.1 will slove the problem.

dty717 avatar Jun 29 '22 07:06 dty717

I have the same error running it with your map_view_simple_example.py code. I am using mac 12.0.1.

neel-dev avatar Jul 24 '22 17:07 neel-dev

I have this same issue too even after upgrading pillow. It seems to only happen if I don't use the default mainloop like BlaatVogel noted. It works fine when I use mainloop - however my (existing) script is using cefpython3, so I'm running a custom main loop that looks like this:

root.update_idletasks()
root.update()
sleep(5/1000)
cef.MessageLoopWork()

to combine both the tkinter GUI loop with the cef message loop. I think it's possible to have cef run on another thread, but I'll probably be getting rid of it anyways if this map widget works for me, as I was using the integrated browser to display an HTML map file anyways :)

What's relying on the mainloop that's causing an issue with PhotoImage?

birdonwheels5 avatar Jul 24 '22 20:07 birdonwheels5

It looks like the loop is still running but parts of the PhotoImage class don't exist anymore when the program gets closed. Like if they are garbage collected earlier than the loop, I don't know. I will try to put in some more try except blocks to catch these.

TomSchimansky avatar Jul 24 '22 21:07 TomSchimansky

I also gets the same error :-(

alopezme avatar Apr 30 '24 13:04 alopezme