首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python和tkPDFViewer进行缩放PDF查看器?

如何使用Python和tkPDFViewer进行缩放PDF查看器?
EN

Stack Overflow用户
提问于 2022-01-11 08:09:34
回答 1查看 1.3K关注 0票数 0

我制作了一个非常简单的PDF查看器,用于存放可执行文件所在文件夹中的所有PDF文件。然后,我对向按钮面板添加一些基本实用程序感兴趣,例如使用缩放+和缩放按钮来增加PDF文件的视图,并允许改进所选PDF文件的读取。

这是我的代码https://github.com/marco-rosso-m/GUI_simplePDFviewer

代码语言:javascript
复制
import tkinter as tk
from tkPDFViewer import tkPDFViewer as pdf 
from os import path
from glob import glob
import sys
import os

# set global variable v2
global v2
v2 = None

def list_file_ext_current_folder(dr, ext, ig_case=False):
    if ig_case: # case sensitive
        ext =  "".join(["[{}]".format(ch + ch.swapcase()) for ch in ext])
    return glob(path.join(dr, "*." + ext))

def on_select(evt):
    global v2
    # Note here that Tkinter passes an event object to onselect()
    w = evt.widget
    index = int(w.curselection()[0])
    value = w.get(index)
    # print('You selected item %d: "%s"' % (index, value))
    if v2: # if old instance exists, destroy it first
        v2.destroy()
    # creating object of ShowPdf from tkPDFViewer. 
    v1 = pdf.ShowPdf() 
    # clear the image list # this corrects the bug inside tkPDFViewer module
    v1.img_object_li.clear()
    # Adding pdf location and width and height. 
    v2=v1.pdf_view(frame2,pdf_location = f"{value}",  width = 80, height = 100)
    # Placing Pdf inside gui
    v2.pack()

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(
        sys,
        '_MEIPASS',
        os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

# tkinter root widnow
root = tk.Tk() 
root.geometry('800x600')
root.minsize(800, 600)
root.title('Simple PDF Viewer inside the current Folder')
root.wm_iconbitmap(resource_path('logo.ico'))

# tkinter frame for listbox
frame1=tk.Frame(root,width=40)
# tkinter frame for pdf viewer
frame2=tk.Frame(root)

frame1.pack(side="left",fill="both",expand=True)
frame2.pack(side="right",fill="both",expand=True)

# get list of pdf files inside the current folder
paths=list_file_ext_current_folder(".","pdf",True)

# vertical scrollbar
yscrollbar = tk.Scrollbar(frame1)
yscrollbar.pack(side = tk.RIGHT, fill = tk.Y)

# generate listbox
lb = tk.Listbox(frame1, selectmode = "SINGLE", name='lb', yscrollcommand = yscrollbar.set)

lb.pack(padx = 10, pady = 10, expand = tk.YES, fill = "both")

if paths: # if paths list is not empty
    for each_item in range(len(paths)):
        lb.insert(tk.END, paths[each_item])
        lb.itemconfig(each_item)
    lb.bind('<<ListboxSelect>>', on_select)
    lb.select_set(0) # This only sets focus on the first item.
    lb.event_generate("<<ListboxSelect>>") # This creates the even clicked
else:
    lb.insert(tk.END, "No PDF are present in this forlder!")

# Attach listbox to vertical scrollbar
yscrollbar.config(command = lb.yview)


root.mainloop()

此外,目前,上述已实现的PDF浏览器似乎对窗口维度没有响应性,是否有办法使其响应?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-19 09:07:19

为了使用tkPDFViewer模块管理缩放,我下载了源代码这里,然后修改了pdf_view函数,添加了一个我称为zoomDPI的缩放参数,设置了它的默认值72 the:

代码语言:javascript
复制
def pdf_view(self,master,width=1200,height=600,pdf_location="",bar=True,load="after",zoomDPI=72):

然后,使用新的输入参数来修改函数getPixmap()中的方法add_img()。在此函数中,我修改了以下行,插入新的输入参数,该参数控制图像的分辨率,从而控制缩放级别:

代码语言:javascript
复制
pix = page.getPixmap(dpi=zoomDPI)

最后,在主代码中,当调用pdf_view方法时,我可以通过输入一个较高的dpi值72来调整图像级别,以增加缩放级别(放大),或者给出一个较低的值,以降低缩放级别(放大)。

代码语言:javascript
复制
        if v2: # if old instance exists, destroy it first
            v2.destroy()
        # creating object of ShowPdf from tkPDFViewer. 
        v1 = pdf.ShowPdf() 
        # clear the image list # this corrects the bug inside tkPDFViewer module
        v1.img_object_li.clear()
        # Adding pdf location and width and height. 
        v2=v1.pdf_view(frame2,pdf_location = f"{value}",zoomDPI=zoomDPI) # default value for zoomDPI=72. Set higher dpi for zoom in, lower dpi for zoom out
        # Placing Pdf inside gui
        v2.pack()

新的完整代码可以找到这里

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70663492

复制
相关文章

相似问题

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