# 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 :
- Run the code above once, It will display the .PDF in the window.
- Close the Window.
- Run again the code
- 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 ?
CodePudding user response:
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()