基於 Tkinter 在特定文字行中根據關鍵詞突出顯示顏色
建立有效的 GUI 通常涉及顯示和操作文字。Tkinter 是流行的 Python GUI 工具包,它提供了一個通用的 Text 元件來處理基於文字的內容。在本教程中,我們將展示如何在 Tkinter 應用程式中根據特定條件突出顯示特定文字行。
設定 Tkinter 環境
讓我們從設定基本的 Tkinter 環境開始。確保已安裝 Tkinter,方法是執行 -
pip install tk
建立簡單的 Tkinter 視窗
現在,讓我們建立一個帶有 Text 元件的簡單 Tkinter 視窗 -
import tkinter as tk # Create the Tkinter window root = tk.Tk() root.title("Highlighting colour for a specific line of text") root.geometry("720x250") # Create a Text widget text_widget = tk.Text(root, wrap=tk.WORD) text_widget.pack(expand=True, fill=tk.BOTH) # Start the Tkinter event loop root.mainloop()
這構成了我們文字突出顯示演示的基礎。
Tkinter 中的文字突出顯示
要根據關鍵詞突出顯示特定行,我們將建立一個名為 highlight_keyword 的函式。此函式將與 Text 元件互動,將標籤應用於文字的相關部分 -
def highlight_keyword(text_widget, keyword, line_number, fg_color='yellow', bg_color='red'): start_index = text_widget.index(f'{line_number}.0') end_index = text_widget.index(f'{line_number + 1}.0') line_text = text_widget.get(start_index, end_index) if keyword in line_text: start_index = start_index + '+%dc' % line_text.index(keyword) end_index = start_index + '+%dc' % len(keyword) text_widget.tag_add('highlight', start_index, end_index) text_widget.tag_config('highlight', foreground=fg_color, background=bg_color)
分解 highlight_keyword 函式
確定行索引 - 在 Text 元件中獲取指定行的起始和結束索引。
檢索行文字 - 提取指定行的文字內容。
檢查關鍵詞是否存在 - 驗證該行中是否存在關鍵詞。
應用標籤 - 如果找到關鍵詞,則計算關鍵詞在該行中的索引,並將標籤('highlight')應用於該範圍。
配置標籤樣式 - 自定義標籤的外觀,指定前景色和背景色。
在 Tkinter 中結合文字插入和關鍵詞突出顯示
現在,讓我們將文字插入和關鍵詞突出顯示結合到我們的 Tkinter 環境中 -
# Insert some text into the Text widget text_widget.insert(tk.END, "This is the first line.\n") text_widget.insert(tk.END, "This line contains the keyword.\n") text_widget.insert(tk.END, "This is the third line.\n") text_widget.insert(tk.END, "This is the fourth line.\n") text_widget.insert(tk.END, "This is the fifth line.\n") text_widget.insert(tk.END, "This is the sixth line.\n") # Highlight the line containing the keyword highlight_keyword(text_widget, 'keyword', 2)
將所有內容整合在一起
讓我們將所有這些整合在一起並檢查輸出 -
示例
import tkinter as tk def highlight_keyword( text_widget, keyword, line_number, fg_color='yellow', bg_color='red'): start_index = text_widget.index(f'{line_number}.0') end_index = text_widget.index(f'{line_number + 1}.0') line_text = text_widget.get(start_index, end_index) if keyword in line_text: start_index = start_index + '+%dc' % line_text.index(keyword) end_index = start_index + '+%dc' % len(keyword) text_widget.tag_add('highlight', start_index, end_index) text_widget.tag_config( 'highlight', foreground=fg_color, background=bg_color ) # Create the Tkinter window root = tk.Tk() root.title("Highlighting colour for a specific line of text") root.geometry("720x250") # Create a Text widget text_widget = tk.Text(root, wrap=tk.WORD) text_widget.pack(expand=True, fill=tk.BOTH) # Insert some text into the Text widget text_widget.insert(tk.END, "This is the first line.\n") text_widget.insert(tk.END, "This line contains the keyword.\n") text_widget.insert(tk.END, "This is the third line.\n") text_widget.insert(tk.END, "This is the fourth line.\n") text_widget.insert(tk.END, "This is the fifth line.\n") text_widget.insert(tk.END, "This is the sixth line.\n") # Highlight the line containing the keyword highlight_keyword(text_widget, 'keyword', 2) # Start the Tkinter event loop root.mainloop()
這裡,六行文字被插入到 Text 元件中。然後呼叫 highlight_keyword 函式來突出顯示包含關鍵詞“keyword”的行。
輸出
執行此程式碼後,您將獲得以下輸出視窗 -

結論
Tkinter 中的文字突出顯示增強了圖形應用程式的可用性。使用 Text 元件和標記機制,開發人員可以根據各種條件實現動態且視覺上吸引人的文字突出顯示。
廣告