首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tkPDFViewer只工作一次然后抛出:图像"pyimage1“不存在

tkPDFViewer只工作一次然后抛出:图像"pyimage1“不存在
EN

Stack Overflow用户
提问于 2021-12-09 15:34:52
回答 1查看 636关注 0票数 0
代码语言:javascript
复制
# 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:

  1. 运行上述代码一次,它将在窗口中显示.PDF。
  2. 关闭窗口。
  3. 再次运行代码
  4. ,然后将得到以下问题:

< code >G 210

代码语言:javascript
复制
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

有人知道我怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-09 16:47:40

tkPDFViewer使用类变量img_object_li (类型list)存储从PDF文件中提取的图像。在spyder中执行脚本时,即使关闭tkinter窗口,类tkPDFViewer的实例仍然存在于spyder内核中。

因此,下一次执行脚本时,前面的映像实例仍然存储在类变量img_object_li中,并且它们将被再次使用和显示,这会引发异常,因为以前的Tk()实例已被销毁。

要解决这个问题,类变量img_object_li在加载PDF文件之前应该是清楚的:

代码语言:javascript
复制
# 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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70292588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档