here is my python app. i am able to open the search result file. but after opening the file and going back to the app the app doesnt respond. but after closing the file it responds. heres my  code.
please fix that issue

import os
import tkinter as tk
from tkinter import filedialog, messagebox
import chardet
import configparser
import threading
import re
import sys
import time
import subprocess

pause_task = None
paused_search_var = None


def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


def browse_files():
    filetypes = [("ATM Journals", "*.jrn"), ("All files", "*.*")]
    files = filedialog.askopenfilenames(title="Select files", filetypes=filetypes)
    if not files:
        return
    directory_entry.delete(0, tk.END)
    directory_entry.insert(0, "||".join(files))


search_started = False
search_terminated = False


def search_files():
    global search_started

    search_started = True
    search_terminated.set(False)

    files_str = directory_entry.get()
    keywords = keywords_entry.get()

    if not files_str or not keywords:
        error_message = ""
        if not files_str:
            error_message = "You have not selected any journals."
        if not keywords:
            if error_message:
                error_message += "\n"
            error_message += "You have not added any keywords."
        messagebox.showerror("Error", error_message)
        return

    config = configparser.ConfigParser()
    config.read("keywords.ini")
    if "Keywords" not in config.sections():
        config.add_section("Keywords")
    config.set("Keywords", "search_keywords", keywords)
    with open("keywords.ini", "w") as configfile:
        config.write(configfile)

    files = files_str.split("||")

    if not files or not keywords:
        message = "Please select files and enter keywords to search.\n\n"
        messagebox.showerror("Error", message)
        return

    results_listbox.delete(0, tk.END)
    content_text.delete(1.0, tk.END)

    paused_search_var.set(False)

    search_started = True

    file_path = None

    def search():
        nonlocal file_path
        for file_path in files:
            if search_terminated.get():
                break

            while paused_search_var.get():
                time.sleep(0.1)

            if os.path.isfile(file_path):
                content = None
                with open(file_path, "rb") as f:
                    raw_data = f.read()
                    detected_encoding = chardet.detect(raw_data)["encoding"]
                try:
                    with open(file_path, "r", encoding=detected_encoding) as f:
                        content = f.read()
                except UnicodeDecodeError:
                    with open(file_path, "r", encoding="ISO-8859-1") as f:
                        content = f.read()

                content_lower = content.lower()

                for keyword in keywords.split(","):
                    keyword = keyword.strip()
                    keyword_lower = keyword.lower()

                    start = content_lower.find(keyword_lower)
                    while start != -1:
                        end = start + len(keyword)
                        result = f"{file_path}: {keyword}: {start}-{end}"
                        results_listbox.insert(tk.END, result)
                        start = content_lower.find(keyword_lower, end)

            if search_terminated.get():
                break

    search_thread = threading.Thread(target=search)
    search_thread.start()

    while search_thread.is_alive():
        app.update()
        time.sleep(0.1)
        if search_terminated.get():
            break

    if not results_listbox.get(0, tk.END):
        messagebox.showinfo("Message", "No results were found.")
    else:
        messagebox.showinfo("Message", "Search completed.")

    pause_button.config(text="Pause")
    paused_search_var.set(False)

    if instance_data:
        display_instance(-1, file_path)

    search_started = False


def index_to_line_col(text_widget, index):
    return tuple(map(int, text_widget.index(index).split(".")))


instance_data = []


def show_file_content(event):
    selection = results_listbox.curselection()
    if not selection:
        return

    result = results_listbox.get(selection[0])
    filename, keyword, indices_str = result.split(": ")
    indices = tuple(map(int, indices_str.split("-")))
    instance_data.append((filename, keyword, indices))

    file_path = filename
    display_instance(-1, file_path)


def open_file(event):
    selection = results_listbox.curselection()
    if not selection:
        return

    result = results_listbox.get(selection[0])
    filename, keyword, indices_str = result.split(": ")

    file_path = os.path.join(directory_entry.get(), filename)
    subprocess.run(["notepad.exe", file_path], check=True)


def display_instance(instance_index, file_path):
    filename, keyword, indices = instance_data[instance_index]

    def read_file():
        with open(file_path, "r", encoding="ISO-8859-1") as file:
            content = file.read()
        return content

    def highlight_keyword(content):
        keyword_pattern = re.compile(re.escape(keyword), re.IGNORECASE)
        match = keyword_pattern.search(content, indices[0])

        if match:
            start = match.start()
            end = match.end()
            start_line_col = content_text.index(f"1.0+{start}c")
            end_line_col = content_text.index(f"1.0+{end}c")
            content_text.tag_remove("highlight", "1.0", tk.END)
            content_text.tag_add("highlight", start_line_col, end_line_col)
            content_text.tag_configure("highlight", background="yellow")

            content_text.see(start_line_col)

    content_text.delete(1.0, tk.END)
    content_text.insert(tk.END, "Loading...")

    def update_content():
        content = read_file()
        content_text.delete(1.0, tk.END)
        content_text.insert(tk.END, content)
        highlight_keyword(content)

    thread = threading.Thread(target=update_content)
    thread.start()


def start_search():
    search_thread = threading.Thread(target=search_files)
    search_thread.start()


def pause_search():
    global search_started, paused_search_var

    if not search_started:
        messagebox.showerror("Error", "Search process has not begun.")
        return

    if paused_search_var.get():
        pause_button.config(text="Pause")
        paused_search_var.set(False)
    else:
        if not results_listbox.get(0, tk.END):
            return
        pause_button.config(text="Resume")
        paused_search_var.set(True)


def reset():
    global search_started

    if search_started:
        if not paused_search_var.get():
            messagebox.showerror(
                "Error", "Please pause the current search before resetting."
            )
        else:
            search_terminated.set(True)
            search_started = False
            directory_entry.delete(0, tk.END)
            results_listbox.delete(0, tk.END)
            content_text.delete(1.0, tk.END)

            instance_data.clear()

            search_started = False

            search_terminated.set(False)
    else:
        directory_entry.delete(0, tk.END)
        results_listbox.delete(0, tk.END)
        content_text.delete(1.0, tk.END)

        instance_data.clear()

        search_started = False


def close_app():
    global search_started

    if search_started:
        search_terminated.set(True)

    if messagebox.askyesno("Exit", "Do you want to close the application?"):
        app.destroy()


def show_about():
    messagebox.showinfo("About", "Coded by U2816.")


app = tk.Tk()
app.protocol("WM_DELETE_WINDOW", lambda: close_app())
app.title("ATM Journal Searcher")
app.geometry("637x581")
app.resizable(False, False)
icon_path = resource_path("icon.ico")
app.iconbitmap(icon_path)

paused_search_var = tk.BooleanVar()
search_terminated = tk.BooleanVar()

directory_label = tk.Label(app, text="Directory:")
directory_label.place(x=10, y=10)

directory_entry = tk.Entry(app, width=70)
directory_entry.place(x=80, y=12)

browse_button = tk.Button(app, text="Browse", width=15, command=browse_files)
browse_button.place(x=510, y=10)

keywords_label = tk.Label(app, text="Keywords:")
keywords_label.place(x=10, y=40)

keywords_entry = tk.Entry(app, width=70)
keywords_entry.place(x=80, y=42)

search_button = tk.Button(app, text="Search", width=15, command=search_files)
search_button.place(x=510, y=40)

results_scrollbar = tk.Scrollbar(app)
results_scrollbar.place(x=610, y=70, height=146)

results_listbox = tk.Listbox(
    app, width=100, height=9, yscrollcommand=results_scrollbar.set
)
results_listbox.place(x=10, y=70)
results_scrollbar.config(orient="vertical", command=results_listbox.yview)
results_listbox.bind("<<ListboxSelect>>", show_file_content)
results_listbox.bind("<Double-1>", open_file)


content_label = tk.Label(app, text="Content:")
content_label.place(x=10, y=220)

content_text = tk.Text(app, wrap=tk.WORD, width=75, height=18)
content_text.place(x=10, y=244)

scrollbar = tk.Scrollbar(app, command=content_text.yview)
scrollbar.place(x=610, y=244, height=293)

content_text.config(yscrollcommand=scrollbar.set)

reset_search_button = tk.Button(app, text="Reset", width=15, command=reset)
reset_search_button.place(x=10, y=545)

pause_button = tk.Button(app, text="Pause", width=15, command=pause_search)
pause_button.place(x=130, y=545)

about_button = tk.Button(app, text="About", width=15, command=show_about)
about_button.place(x=250, y=545)

instruction_label = tk.Label(
    app, text="(Single-click to view content, Double-click to open file)"
)
instruction_label.place(x=163, y=220)

config = configparser.ConfigParser()
config.read("keywords.ini")
if "Keywords" in config.sections() and "search_keywords" in config.options("Keywords"):
    keywords_entry.insert(0, config.get("Keywords", "search_keywords"))

app.mainloop()