簡體   English   中英

我需要 Python 的幫助,將 2D 集的內容顯示到 Tkinter 文本框中

[英]I need help in Python with displaying the contents of a 2D Set into a Tkinter Textbox

免責聲明:我才剛剛開始了解 Python。 大約一個月前,我參加了速成課程來學習最基本的知識,而我努力學習的 rest 都是通過 Google 進行的研究,並在 Stack Overflow 中查看解決方案。

我正在嘗試創建一個應用程序,它將讀取存儲在文件夾中的所有 PDF 文件並提取它們的文件名、頁碼和第一頁的內容,並將此信息存儲到 2D 集中。 完成后,應用程序將創建一個帶有 2 個列表框和 1 個文本框的 tkinter GUI。 應用程序應在第一個列表框中顯示 PDF 文件名,並在第二個列表框中顯示每個文件的相應頁碼。 兩個列表框在滾動時同步。

文本框應顯示 PDF 第一頁上的文本內容。 我想要發生的是,每次我用鼠標或向上或向下箭頭鍵在第一個列表框中單擊 PDF 文件名時,應用程序應在文本框中顯示所選文件第一頁的內容。

這就是我的 GUI 的外觀以及它應該如何 function

https://i.stack.imgur.com/xrkvo.jpg

到目前為止,我已經成功滿足所有其他要求,除了當我在第一個列表框中 select 文件名時,PDF 的第一頁的內容應該顯示在文本框中。

這是我用於填充列表框和文本框的代碼。 我的 2D 集 pdfFiles 的內容是 [['PDF1 filename', 'PDF1 total pages', 'PDF1 text content of first page'], ['PDF2 filename', 'PDF2 total pages', 'PDF2 text content of first page '], ... ETC。

===========設置列表框和文本框=========

scrollbar = Scrollbar(list_2)
scrollbar.pack(side=RIGHT, fill=Y)
list_1.config(yscrollcommand=scrollbar.set)
list_1.bind("<MouseWheel>", scrolllistbox2)
list_2.config(yscrollcommand=scrollbar.set)
list_2.bind("<MouseWheel>", scrolllistbox1)
txt_3 = tk.Text(my_window, font='Arial 10', wrap=WORD)
txt_3.place(relx=0.5, rely=0.12, relwidth=0.472, relheight=0.86)
scrollbar = Scrollbar(txt_3)
scrollbar.pack(side=RIGHT, fill=Y)
list_1.bind("<<ListboxSelect>>", CurSelect)

============用二維集的內容填充列表框===

i = 0
while i < count:
    list_1.insert(tk.END, pdfFiles[i][0])
    list_2.insert(tk.END, pdfFiles[i][1])
    i = i + 1

============這是我的 CurSelect 函數代碼========

def CurSelect(evt):
    values = [list_1.get(idx) for idx in list_1.curselection()]
    print(", ".join(values)) ????

=========================

上面的打印命令只是我的測試命令,表明我已經成功提取了列表框中的選定項。 我現在需要的是以某種方式將該信息鏈接到我的 2D 列表中相應的頁面內容,並將其顯示在文本框中。

Something like:
1) select the filename in the listbox
2) link the selected filename to the filenames stored in the pdfFilename 2D set
3) once filename is found, identify the corresponding text of the first page
4) display the text of the first page of the selected file in the text box

我希望我說得通。 請幫忙。

你不需要太多就可以完成它。 您只需要一些小東西: 1. 獲取listbox的選定項:

selected_indexes = list_1.curselection()
first_selected = selected_indexes[0]  # it's possible to select multiple items

2.獲取對應的PDF文本:

pdf_text = pdfFiles[first_selected][2]

3. 更改Text小部件的文本:(來自https://stackoverflow.com/a/20908371/8733066

txt_3.delete("1.0", tk.END)
txt_3.insert(tk.END, pdf_text)

所以用這個替換你的CurSelect(evt)方法:

def CurSelect(evt):
    selected_indexes = list_1.curselection()
    first_selected = selected_indexes[0]
    pdf_text = pdfFiles[first_selected][2]
    txt_3.delete("1.0", tk.END)
    txt_3.insert(tk.END, pdf_text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM