'tkPDFViewer working only once then throw : image "pyimage1" doesn't exist

# Importing tkinter to make gui in python
from tkinter import*
  
# Importing tkPDFViewer to place pdf file in gui.
# In tkPDFViewer library there is
# an tkPDFViewer module. That I have imported as pdf
from tkPDFViewer import tkPDFViewer as pdf
  
v2= NONE
# Initializing tk
root = Tk()
  
# Set the width and height of our root window.
root.geometry("550x750")
  
# creating object of ShowPdf from tkPDFViewer.
v1 = pdf.ShowPdf()
  
# Adding pdf location and width and height.
v2 = v1.pdf_view(root,
                 pdf_location = r"YourPdfFile.pdf", 
                 width = 50, height = 100)
  
# Placing Pdf in my gui.
v2.pack()
root.mainloop()

How to reproduce the bug :

  1. Run the code above once, It will display the .PDF in the window.
  2. Close the Window.
  3. Run again the code
  4. Then you will get the following issue :

.

Exception in thread Thread-10:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tkpdfviewer-0.1-py3.9.egg\tkPDFViewer\tkPDFViewer.py", line 61, in add_img
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 3728, in image_create
    return self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist

Does someone knows how I can fix this ?



Solution 1:[1]

tkPDFViewer use a class variable img_object_li (type list) to store the images extracted from the PDF file. When you execute the script in spyder, the instance of the class tkPDFViewer still exists in the spyder kernel even after the tkinter window is closed.

So the next time the script is executed, the previous instances of the images still stored in the class variable img_object_li and they will be used and shown again which raised the exception because the previous instance of Tk() is destroyed.

To fix the issue, the class variable img_object_li should be clear before loading the PDF file:

# Importing tkinter to make gui in python
from tkinter import*
  
# Importing tkPDFViewer to place pdf file in gui.
# In tkPDFViewer library there is
# an tkPDFViewer module. That I have imported as pdf
from tkPDFViewer import tkPDFViewer as pdf
  
v2= NONE
# Initializing tk
root = Tk()
  
# Set the width and height of our root window.
root.geometry("550x750")
  
# creating object of ShowPdf from tkPDFViewer.
v1 = pdf.ShowPdf()

### clear the image list ###
v1.img_object_li.clear()
  
# Adding pdf location and width and height.
v2 = v1.pdf_view(root,
                 pdf_location = r"test.pdf", 
                 width = 50, height = 100)
  
# Placing Pdf in my gui.
v2.pack()
root.mainloop()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 acw1668