我的代码
from tkinter import *
import tkinter as tk
from tkinter.filedialog import *
from tkPDFViewer import tkPDFViewer as pdf
def donothing():
x = 0
def open():
file=askopenfile()
v1 = pdf.ShowPdf()
v2 = v1.pdf_view(root,
pdf_location = file,
width = 200, height = 100)
v2.pack()
root = Tk()
root.state('zoomed')
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=open)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
root.mainloop()如果您打开第一个pdf文件,它是word非常good.But,当您打开另一个pdf文件时,它不是update.So,我如何修复它?谢谢
发布于 2021-09-10 13:00:10
首先,每次调用pdf_view(...)时,都会创建tkinter Frame的一个新实例,并创建pack(),因此Frame的第二个实例打包在第一个实例下面。但是由于第一个Frame有点长,第二个在可视区域之外,所以您看不到它。您可以通过使用较小的高度来查看第二个框架,例如,从100更改为50,然后可以看到这两个框架。
要解决这个问题,您需要销毁Frame的第一个实例。此外,tkPDFViewer使用类变量img_object_li ( list)存储所有图像(从PDF文件中提取),因此在打开另一个PDF文件之前需要清除它:
v2 = None # change v2 to global variable
def open():
global v2
file = askopenfile()
if file:
# if old instance exists, destroy it first
if v2:
v2.destroy()
v1 = pdf.ShowPdf()
# clear the stored image list
v1.img_object_li.clear()
# shows the new images extracted from PDF file
v2 = v1.pdf_view(root, pdf_location=file, width=200, height=50) # smaller height
v2.pack()https://stackoverflow.com/questions/69131765
复制相似问题