Tkinter:在GUI中跳过PDF

2024-09-29 23:30:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,希望构建一个gui,让您可以跳过不同的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()

当我点击“跳过”按钮时,“父路径”中的下一个pdf应该出现在显示初始pdf的位置

谢谢你的帮助

弗洛


Tags: fromimportdocpdfshowcounterlocationroot
1条回答
网友
1楼 · 发布于 2024-09-29 23:30:19

我已经研究了tkPDFViewer's code,并且ShowPdf类并没有被设计为在初始文档之后加载新文档

第二个文件显示在第一个文件旁边的原因是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类,而不是PIL类)

相关问题 更多 >

    热门问题