Home > Blockchain >  How to update Pdf_view in tkinter?
How to update Pdf_view in tkinter?

Time:09-17

My code

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()

If you open first pdf file it's word very good.But when you open a other pdf file then it's not update.So how can I fix it?Thank you

CodePudding user response:

First every time you call pdf_view(...), a new instance of tkinter Frame is created and pack(), so the second instance of Frame is packed below the first one. But since the first Frame is a bit long, the second one is out of the viewable area and so you cannot see it. You can see the second one by using a smaller height, for example, change from 100 to 50, then you can see both the frames.

To fix the issue, you need to destroy the first instance of the Frame. Also tkPDFViewer uses a class variable img_object_li (a list) to stores all the images (extracted from the PDF file), so you need to clear it before opening another PDF file:

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()
  • Related