Tkinter:在 GUI 中跳过 PDF

Tkinter: Skip through PDFs in GUI

我是 python 的新手,想构建一个可以让您跳过不同 pdf 的图形用户界面。我设法显示了第一个 pdf。我现在的问题是我无法显示第二个 pdf 和所有后续内容。如果我只重复显示 pdf 的命令,新 pdf 会显示在旧 pdf 旁边,而不是替换它。我已经通过几个小时的广泛谷歌搜索,但无法找到解决这个问题的方法。有人可以帮忙吗?

这是我的代码:

 from tkinter import *
 import tkinter as tk
 import glob
 from tkPDFViewer import tkPDFViewer as pdf
 from tkdocviewer import *

 parent_path = 'somepath\'
 doc_list = glob.glob((parent_path + "*//*.pdf"))
 doc_counter = 0

 root = tk.Tk()


 root.title('Training Data Creator')
 root.geometry("1000x1000")
 frame_r = Frame(root, relief=RAISED, borderwidth=1)
 frame_r.pack(fill=BOTH, expand=True, side=tk.LEFT)

 # creating object of ShowPdf from tkPDFViewer.
 pdf_show = pdf.ShowPdf()


 # Adding pdf location and width and height.
 V_pdf = pdf_show.pdf_view(master=frame_r,
             pdf_location=(doc_list[doc_counter]),
             width=90, height=100)


 V_pdf.pack(side=tk.LEFT)


 #button skip
 def skip_callback():
     global doc_counter
     doc_counter = doc_counter +1


     # Here I want to display the next PDF!!

     V_pdf = pdf_show.pdf_view(master=frame_r,
                          pdf_location=(doc_list[doc_counter]),
                          width=90, height=100)


     V_pdf.pack()
     print(doc_counter)


 button_skip = Button(root, text='skip', command= skip_callback)
 button_skip.pack(fill=tk.X, pady=0)


 root.mainloop()

当我点击 'skip' 按钮时,'parent_path' 中的下一个 pdf 应该出现在第一个 pdf 显示的位置。

感谢您的帮助!

弗洛

我调查了 tkPDFViewer's code 并且 ShowPdf class 并未设计为在初始文档之后加载新文档。

第二个文件显示在第一个文件旁边的原因是pdf_show.pdf_view()创建了一个新的Frame,然后将其打包在前一个文件的右侧。所以你需要先用

摧毁旧的
pdf_show.frame.destroy()

然后,我注意到点击跳过按钮时显示的不是第二个文档,而是两个文档的串联。这可以通过清除 pdf_show

的图像列表来解决
pdf_show.img_object_li.clear()

完整代码如下:

import tkinter as tk
import glob
from tkPDFViewer import tkPDFViewer as pdf

parent_path = '/home/juliette/Documents'
doc_list = glob.glob((parent_path + "*//*.pdf"))
doc_counter = 0

root = tk.Tk()
root.title('Training Data Creator')
root.geometry("1000x1000")
frame_r = tk.Frame(root, relief=tk.RAISED, borderwidth=1)
frame_r.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)

# creating object of ShowPdf from tkPDFViewer.
pdf_show = pdf.ShowPdf()

# Adding pdf location and width and height.
V_pdf = pdf_show.pdf_view(master=frame_r,
                          pdf_location=(doc_list[doc_counter]),
                          width=90, height=100)
V_pdf.pack(side=tk.LEFT)

#button skip
def skip_callback():
    global doc_counter
    doc_counter = doc_counter +1

    # Reset view:
    pdf_show.frame.destroy()
    pdf_show.img_object_li.clear()
    # Display new pdf
    V_pdf = pdf_show.pdf_view(master=frame_r,
                              pdf_location=(doc_list[doc_counter]),
                              width=90, height=100)
    V_pdf.pack(side=tk.LEFT)
    print(doc_counter)


button_skip = tk.Button(root, text='skip', command= skip_callback)
button_skip.pack(fill=tk.X, pady=0)

root.mainloop()

顺便说一句,您正在导入 tkinter 两次:

 from tkinter import *
 import tkinter as tk

所以我建议您选择其中一种导入方式并坚持使用,而不是混合使用两种方式。此外,我建议您使用 import tkinter as tk,这样您就不会在命名空间中塞满大量可能导致命名冲突的未知名称(例如,如果您从 PIL 导入 Image 然后执行 from tkinter import *Image 将是 tkinter class,而不是 PIL。