首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tkPDFViewer -在网格中打开几个不工作的.pdf (显示相同的pdf并将它们混合在一起)

tkPDFViewer -在网格中打开几个不工作的.pdf (显示相同的pdf并将它们混合在一起)
EN

Stack Overflow用户
提问于 2021-12-10 10:19:18
回答 1查看 419关注 0票数 -1

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

我的问题是,不可能同时使用:.pdf打开不同的tkPDFViewer

可复制的例子如下:

代码语言:javascript
复制
    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吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-10 13:17:12

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

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

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70302939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档