Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Photo Image Error #7

Open
Kromilan opened this issue Apr 1, 2022 · 17 comments
Open

Photo Image Error #7

Kromilan opened this issue Apr 1, 2022 · 17 comments
Labels
bug Something isn't working

Comments

@Kromilan
Copy link

Kromilan commented Apr 1, 2022

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'
@TomSchimansky
Copy link
Owner

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 TomSchimansky added the bug Something isn't working label Apr 1, 2022
@TomSchimansky
Copy link
Owner

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

@Kromilan
Copy link
Author

Thank you greatly for looking into it. :)

@miyucode
Copy link

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.

@TomSchimansky
Copy link
Owner

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

@miyucode
Copy link

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 !

@TomSchimansky TomSchimansky reopened this May 25, 2022
@TomSchimansky
Copy link
Owner

Which operating system are you working on?

@miyucode
Copy link

I'm working on Windows 10 !

@mpageers
Copy link

mpageers commented Jun 20, 2022

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)

@dty717
Copy link

dty717 commented Jun 29, 2022

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

@neel-dev
Copy link

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

@birdonwheels5
Copy link

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?

@TomSchimansky
Copy link
Owner

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.

@alopezme
Copy link

I also gets the same error :-(

@EddieRadarHughes
Copy link

I'm getting the same error. I have version 1.29. I'm using Python 3.9 and IDLE on a Windows 10 Pro computer with lots of horsepower and RAM. If there is an error anywhere in my code, this Photoimage class error keeps running. I have to kill the application completely, which means I can't read what my errors are because they continue to scroll off. I managed to take a snapshot of the error message.

AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Exception ignored in: <function PhotoImage.del at 0x00000154C77141F0>
Traceback (most recent call last):
File "C:\Users....\ImageTk.py", line 118, in del
name = self.__photo.name

I love this package, but it would be nice if the 403 error and this Photoimage error were eliminated.

@EddieRadarHughes
Copy link

EddieRadarHughes commented May 22, 2024

I just fixed my issue with the previously mentioned AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'. This error continues to scroll indefinitely after any other error is triggered in your code. It scrolls so fast that I cannot read my offending error message in the shell. Even CTRL-C and breakpoints are ignored with this AttributeError. I would have to kill the shell to end the program, and thus lose my error messages.

I fixed the problem as follows: I opened ImageTk.py file in the site-packages\PIL folder and moved the "try" statement up by two rows. This put it in front of the problem line -- #118. Now when I get any other error, the code actually stops after that error instead of continuing infinitely with the AttributeError messages. I have not seen any other errors triggered by this edit. It's not elegant, but it has solved my problem.

@emdete
Copy link

emdete commented Oct 6, 2024

i see that error too, i assume that the cause is an exception in during the constructor init before __photo is created (line 126 in my version 10.4.0 of Pillow). this exception isnt logged and thus not visibile. i have no clue where that can happen but could that be a hint for someone knowing the code better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants