【问题标题】:How do I shop tkPDFViewer from combining PDF's when opening PDFs using buttons?使用按钮打开 PDF 时,如何通过组合 PDF 购买 tkPDFViewer?
【发布时间】:2021-07-14 19:28:24
【问题描述】:

在这段代码中,我尝试使用 tkinter 和 tkPDFViewer 将 PDF 作为单独的窗口打开。这样做的方法是打开一个简单的 UI,其中包含 2 个按钮,用于在这种情况下从我的计算机访问的 2 篇文章(但我只是将其标记为文件路径的“文件路径”以避免泄露个人信息)。但是,当按钮工作并打开 PDF 窗口和其下方的下载窗口(如预期的那样)时,当您关闭 PDF 窗口(并且下载窗口随之关闭)然后重新单击按钮(相同一个或一个不同的),它显示了上一个pdf和您重新单击时出现的当前pdf组合。当您重复此过程时,PDF 只是相互附加。但是,应该会显示与按钮中描述的文章对应的 PDF,而没有其他 PDF。我该如何解决?我已经尝试将 PDF 初始化移到事件函数之外然后重新初始化它,但这会导致“在定义之前使用的局部变量”错误,所以这不起作用,我目前不知道任何其他方式来做到这一点。

pdfArray = [
    ["skweak: Weak Supervision Made Easy for NLP",
    r"filepath", 
    "Pierre Lison, Jeremy Barnes, and Aliaksandr Hubin",
    "skweak, NLP, Natural Language Processing"],
    
    ["Information Theoretic-Based Quality Measures for Clustering",
    r"filepath",
    "Barry Drake, and Tiffany Huang",
    "Quality Measures, Clustering, Data"
    ]
]

#Import tkinter library
import os
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkPDFViewer import tkPDFViewer as pdfViewer
from functools import partial
import shutil

#Create an instance of tkinter frame or window
mainWin= Tk()

#Set the geometry of tkinter frame
mainWin.geometry("1000x600+20+20")
download_icon = tk.PhotoImage(file=r'filepath')
paw_icon = tk.PhotoImage(file=r'filepath')

def download_pdf(original_file):
    #this if-else statment detects if the original file exists.
    if os.path.isfile(original_file):
        newFileStem = os.path.expanduser('~') + r"\Downloads\ChatBox_download"
        num = 0
        while os.path.isfile(newFileStem + "(%d).pdf"%num):
           num = num + 1;

        newFile = newFileStem + "(%d).pdf"%num

        f = open(newFile, "x")
        shutil.copyfile(original_file, newFile)


        completeWin = Toplevel(mainWin)
        completeWin.geometry("400x75+660+480")
        completeWin.title("Download Complete")
        Label(completeWin, text = "Download Complete!", font = ('Gabriola', 14, 'bold')).pack()
        Label(completeWin, text = str(newFile), font = ('Gabriola', 10)).pack(pady=2)
        
    else:
        notFoundWin = Toplevel(mainWin)
        notFoundWin.geometry("200x75+660+480")
        notFoundWin.title("File Not Found")
        Label(notFoundWin, text = "File Not Found", font = ('Gabriola', 14, 'bold')).pack(pady=20)


#Define a new function to open the window
def open_win(pdf_ID):
   pdf_title = pdfArray[pdf_ID][0] #title of pdf
   file_location = pdfArray[pdf_ID][1] #location of pdf
   authors = pdfArray[pdf_ID][2] #authors
   keywords = pdfArray[pdf_ID][3] #keywords
    
   pdfWin = Toplevel(mainWin)
   pdfWin.geometry("600x350+640+20")
   pdfWin.title(pdf_title)

   # Adding pdf location and width and height.

   pdfViewer.ShowPdf().pdf_view(pdfWin, pdf_location=file_location, width=70, height=100).pack(ipadx = 10, ipady = 10)

   infoWin = Toplevel(pdfWin)
   infoWin.geometry("600x200+640+420")
   infoWin.title("PDF Information")
   
   Label(infoWin, text = "Information: ", font = ('Gabriola', 15, 'bold')).pack(pady=2)
   Label(infoWin, text = "Title: " + pdf_title, font = ('Gabriola', 12)).pack(pady=1) 
   Label(infoWin, text = "Author(s): " + authors, font = ('Gabriola', 12)).pack(pady=1)
   Label(infoWin, text = "Keyword(s): " + keywords, font = ('Gabriola', 12)).pack(pady=1)
   tk.Button(infoWin, image=download_icon, borderwidth = 0, command=partial(download_pdf, file_location)).pack(pady=1)

    
#Create a label
Label(mainWin, text= "Click any button below to open a PDF", font= ('Gabriola', 25, 'bold')).pack(pady=30, padx = 10)

#Create a button to open a New Window
for ID in range(0, len(pdfArray)):
    tk.Button(mainWin, image=paw_icon, compound=LEFT, text="  Open \" " + pdfArray[ID][0]+" \"", font= ('Gabriola', 12), command=partial(open_win, ID)).pack(pady=2)
    
mainWin.mainloop()

【问题讨论】:

    标签: python python-3.x pdf tkinter


    【解决方案1】:

    我在使用 tkPDFViewer 时也遇到了这个问题。 要修复,我必须进入 tkPDFViewer 模块并添加一行:

    self.img_object_li.clear()
    

    紧跟在该行之后:

        def add_img():
    

    我自己只是一个初学者,所以这是我解决这个问题的方法。 据我了解:

            self.img_object_li.append(timg)
    

    无限期地添加图像,我试图从我的主程序中销毁包含对象的框架和小部件,但根本无法让它工作。问题一直存在。这可能不是最好的方法,其他人会有更好的解决方法。

    【讨论】:

    • 绝妙的答案!非常感谢您。我不是问题的原作者,但这个答案对我帮助很大。谢谢!
    猜你喜欢
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多