如何使用 Python tkinter 和 tkPDFViewer 放大 PDF 查看器?
How to make zoom in PDF viewer with Python tkinter and tkPDFViewer?
我为放置可执行文件的文件夹中包含的所有 PDF 文件制作了一个非常简单的 PDF 查看器。然后,我有兴趣向按钮面板添加一些基本实用程序,例如使用缩放 + 和缩放 - 按钮来增加 PDF 文件的视图并允许改进所选 PDF 文件的阅读。
这是我的代码 [ https://github.com/marco-rosso-m/GUI_simplePDFviewer ]:
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 查看器似乎对 window 维度没有响应,有什么方法可以让它响应吗?
为了使用 tkPDFViewer 模块管理缩放,我下载了源代码 here,然后我修改了 pdf_view
函数,添加了一个我称为 zoomDPI 的缩放参数,并设置了它的默认值72dpi 的:
def pdf_view(self,master,width=1200,height=600,pdf_location="",bar=True,load="after",zoomDPI=72):
此后,新的输入参数用于修改函数add_img()
内部的方法getPixmap()
。在此函数中,我修改了以下行,插入了控制图像分辨率和缩放级别的新输入参数:
pix = page.getPixmap(dpi=zoomDPI)
最后,在主代码中,当调用 pdf_view 方法时,我可以通过输入一个高于 72 的 dpi 值来调整图像级别,以增加缩放级别(放大),或者较低的值以降低缩放级别(缩小)。
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()
可以找到新的完整代码here。
我为放置可执行文件的文件夹中包含的所有 PDF 文件制作了一个非常简单的 PDF 查看器。然后,我有兴趣向按钮面板添加一些基本实用程序,例如使用缩放 + 和缩放 - 按钮来增加 PDF 文件的视图并允许改进所选 PDF 文件的阅读。
这是我的代码 [ https://github.com/marco-rosso-m/GUI_simplePDFviewer ]:
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 查看器似乎对 window 维度没有响应,有什么方法可以让它响应吗?
为了使用 tkPDFViewer 模块管理缩放,我下载了源代码 here,然后我修改了 pdf_view
函数,添加了一个我称为 zoomDPI 的缩放参数,并设置了它的默认值72dpi 的:
def pdf_view(self,master,width=1200,height=600,pdf_location="",bar=True,load="after",zoomDPI=72):
此后,新的输入参数用于修改函数add_img()
内部的方法getPixmap()
。在此函数中,我修改了以下行,插入了控制图像分辨率和缩放级别的新输入参数:
pix = page.getPixmap(dpi=zoomDPI)
最后,在主代码中,当调用 pdf_view 方法时,我可以通过输入一个高于 72 的 dpi 值来调整图像级别,以增加缩放级别(放大),或者较低的值以降低缩放级别(缩小)。
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()
可以找到新的完整代码here。