1

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")
Joe
  • 15
  • 6
  • You can try using the `lift()` and/or `lower()` methods that `Toplevel` windows possess to control their stacking order. Here's some [documentation](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/toplevel.html). – martineau Aug 21 '21 at 23:30

1 Answers1

1

If you look into the source code of tkPDFViwer, you will notice that ShowPdf uses a class variable img_object_li (type list) to store the loaded pages from PDF file. Therefore ShowPdf.img_object_li will hold all the pages from those instances of ShowPdf. I think it is a design bug.

You need to clear the list before loading PDF file:

pdf.ShowPdf.img_object_li.clear() # clear loaded pages
pdf.ShowPdf().pdf_view(newWindow,pdf_location=cur_file,width=75,height=100).pack()
acw1668
  • 40,144
  • 5
  • 22
  • 34