# 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()
如何复制bug:
< code >G 210
。
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
有人知道我怎么解决这个问题吗?
发布于 2021-12-09 08:47:40
tkPDFViewer
使用类变量img_object_li
(类型list
)存储从PDF文件中提取的图像。在spyder
中执行脚本时,即使关闭tkinter窗口,类tkPDFViewer
的实例仍然存在于spyder内核中。
因此,下一次执行脚本时,前面的映像实例仍然存储在类变量img_object_li
中,并且它们将被再次使用和显示,这会引发异常,因为以前的Tk()
实例已被销毁。
要解决这个问题,类变量img_object_li
在加载PDF文件之前应该是清楚的:
# 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()
https://stackoverflow.com/questions/70292588
复制