Untitled
unknown
plain_text
a year ago
2.5 kB
3
Indexable
import tkinter as tk
from tkinter import END
class KeywordHighlighterApp:
def __init__(self, root):
self.root = root
self.root.title("Kiểm tra Keyword và Tô màu")
# Tạo ô Text 1 để nhập đoạn văn bản
tk.Label(root, text="Text 1 (đoạn văn bản):").pack()
self.text1 = tk.Text(root, wrap="word", width=60, height=10, font=("Arial", 12))
self.text1.pack(pady=10)
# Tạo ô Text 2 để nhập keyword, các keyword cách nhau bởi dấu phẩy
tk.Label(root, text="Text 2 (keywords, cách nhau bằng dấu phẩy):").pack()
self.text2 = tk.Text(root, wrap="word", width=60, height=5, font=("Arial", 12))
self.text2.pack(pady=10)
# Nút kiểm tra keyword
self.check_button = tk.Button(root, text="Kiểm tra Keywords", command=self.check_keywords)
self.check_button.pack(pady=10)
def check_keywords(self):
# Xóa tất cả highlight cũ trong text2
self.text2.tag_remove("not_found", "1.0", END)
# Lấy nội dung từ text1 và text2
text1_content = self.text1.get("1.0", END).lower()
text2_content = self.text2.get("1.0", END).strip()
# Tách keywords trong text2 dựa trên dấu phẩy
keywords = [keyword.strip() for keyword in text2_content.split(",")]
# Kiểm tra từng keyword có trong text1 hay không
for keyword in keywords:
if keyword and keyword.lower() not in text1_content:
self.highlight_not_found_keyword(keyword)
def highlight_not_found_keyword(self, keyword):
# Tìm và highlight keyword trong text2 nếu không tìm thấy trong text1
start_index = "1.0"
while True:
# Tìm vị trí keyword trong text2
start_index = self.text2.search(keyword, start_index, stopindex=END, nocase=True)
if not start_index:
break
# Tính toán vị trí kết thúc của keyword
end_index = f"{start_index}+{len(keyword)}c"
# Thêm tag để highlight từ khóa
self.text2.tag_add("not_found", start_index, end_index)
start_index = end_index
# Định nghĩa màu sắc cho tag "not_found"
self.text2.tag_config("not_found", background="yellow")
# Chạy ứng dụng
if __name__ == "__main__":
root = tk.Tk()
app = KeywordHighlighterApp(root)
root.mainloop()
Editor is loading...
Leave a Comment