tkPDFViewer - 在网格中打开几个 .pdf 不起作用(显示相同的 pdf 并混合所有这些)

tkPDFViewer - Open several .pdf in grid not working (display same pdf with a mix of all of them)

我的目标是在 tkinter 中同时显示多个 PDF。

我的问题是似乎无法同时打开不同的 .pdf 使用:tkPDFViewer

可重现的例子如下:

    from tkPDFViewer import tkPDFViewer as pdf

    FramePdfMacro = tk.Frame(self,bg='white')
    FramePdfMacro.pack(expand=True, fill=BOTH,pady=2,padx=2)
    
    FramePdfMacro.grid_rowconfigure(0, weight=1)
    Grid.rowconfigure(FramePdfMacro, 0, weight=1)
    Grid.columnconfigure(FramePdfMacro, 0, weight=1)
    Grid.columnconfigure(FramePdfMacro, 1, weight=1)
    
    PDF1 = pdf.ShowPdf()
    PDFv1 = PDF1.pdf_view(FramePdfMacro, pdf_location = "PDF1.pdf",width = 100, height = 150)
    PDFv1.grid(row=0,column=0,sticky="nsew")    
    
    PDF2 = pdf.ShowPdf()
    PDFv2 = PDF2.pdf_view(FramePdfMacro, pdf_location = "PDF2.pdf",width = 100,height = 150)
    PDFv2.grid(row=0,column=1,sticky="nsew")

一旦你运行这个代码,你将得到那些 2.pdf

的两倍

有谁知道如何同时显示两个不同的.pdf

对于tkPDFViewer的当前设计,您不能同时显示两个PDF文件。

由于 tkPDFViewer 使用 pyMuPDF 模块,您可以使用以下相同的模块创建 PDF 查看器:

import tkinter as tk
from tkinter.scrolledtext import ScrolledText
import fitz

class PDFViewer(ScrolledText):
    def show(self, pdf_file):
        self.delete('1.0', 'end') # clear current content
        pdf = fitz.open(pdf_file) # open the PDF file
        self.images = []   # for storing the page images
        for page in pdf:
            pix = page.get_pixmap()
            pix1 = fitz.Pixmap(pix, 0) if pix.alpha else pix
            photo = tk.PhotoImage(data=pix1.tobytes('ppm'))
            # insert into the text box
            self.image_create('end', image=photo)
            self.insert('end', '\n')
            # save the image to avoid garbage collected
            self.images.append(photo)

root = tk.Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure((0,1), weight=1)

pdf1 = PDFViewer(root, width=80, height=30, spacing3=5, bg='blue')
pdf1.grid(row=0, column=0, sticky='nsew')
pdf1.show('test.pdf')

pdf2 = PDFViewer(root, width=80, height=30, spacing3=5, bg='blue')
pdf2.grid(row=0, column=1, sticky='nsew')
pdf2.show('temp.pdf')

root.mainloop()