我制作了一个非常简单的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浏览器似乎对窗口维度没有响应性,是否有办法使其响应?
发布于 2022-01-19 09:07:19
为了使用tkPDFViewer模块管理缩放,我下载了源代码这里,然后修改了pdf_view函数,添加了一个我称为zoomDPI的缩放参数,设置了它的默认值72 the:
def pdf_view(self,master,width=1200,height=600,pdf_location="",bar=True,load="after",zoomDPI=72):然后,使用新的输入参数来修改函数getPixmap()中的方法add_img()。在此函数中,我修改了以下行,插入新的输入参数,该参数控制图像的分辨率,从而控制缩放级别:
pix = page.getPixmap(dpi=zoomDPI)最后,在主代码中,当调用pdf_view方法时,我可以通过输入一个较高的dpi值72来调整图像级别,以增加缩放级别(放大),或者给出一个较低的值,以降低缩放级别(放大)。
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()新的完整代码可以找到这里。
https://stackoverflow.com/questions/70663492
复制相似问题