I have a function checking if a tkinter listbox element is a pdf file. If so, it should be shown via "ShowPdf" in a new window. I only want to view the one pdf I have chosen.
Using the function once works fine. But if I switch to another listbox element and run the function again, the pdf from the first run and the second run are arranged behind each other. So I see the pdf from the first run at first followed by the second pdf. If I run it a third time, then all three pdfs are arranged behind each other etc.
Could you please help me so that only the current pdf is shown?
import tkinter as tk
from tkPDFViewer import tkPDFViewer as pdf
def OnEntryLeft(event):
cur_file=listb.get(listb.curselection())
if (cur_file[-4:]==".pdf" or cur_file[-4:]==".PDF"):
newWindow = tk.Toplevel(root)
pdf.ShowPdf().pdf_view(newWindow,pdf_location=cur_file,width=75,height=100).pack()
else:
messagebox.showinfo("PDF-Check", "NO pdf")
If you look into the source code of
tkPDFViwer, you will notice thatShowPdfuses a class variableimg_object_li(typelist) to store the loaded pages from PDF file. ThereforeShowPdf.img_object_liwill hold all the pages from those instances ofShowPdf. I think it is a design bug.You need to clear the list before loading PDF file: