the rename header does not work when i clik it: import tkinter as tk
from tkinter import simpledialog, messagebox
from tksheet import Sheet
import json
from pathlib import Path
import ctypes

BACKROUND_COLOR = '#FFFFFF'
SIDEBAR_COLOR = '#F7F8FC'
BUTTON_COLOR = '#F7F8FC'
BUTTON_COLOR_SELECTED = '#EAEDEF'
BUTTON_TEXT_COLOR = '#000000'

def set_dpi_awareness():
    """Enable DPI awareness to ensure high DPI settings are supported."""
    try:
        ctypes.windll.shcore.SetProcessDpiAwareness(1)
    except Exception as e:
        print(f"Error setting DPI awareness: {e}")

class TableApp:
    def __init__(self, root):
        """Initialize the Table Editor application."""
        self.root = root
        self.root.title("Table Editor with Sidebar")
        self.root.geometry("1280x720")

        self.sidebar = tk.Frame(root, width=200, bg=SIDEBAR_COLOR, bd=0, relief='flat')
        self.sidebar.pack(expand=False, fill='y', side='left', anchor='nw')

        self.separator = tk.Frame(root, width=100, bg=BACKROUND_COLOR, bd=0, relief='flat')
        self.separator.pack(expand=False, fill='y', side='left', anchor='nw')

        self.main_frame = tk.Frame(root)
        self.main_frame.pack(expand=True, fill='both', side='right')

        self.header_frame = tk.Frame(self.main_frame, height=50, bg=BACKROUND_COLOR)
        self.header_frame.pack(fill='x')

        self.filter_label = tk.Label(self.header_frame, text="Filter:", bg=BACKROUND_COLOR, anchor="w")
        self.filter_label.pack(side='left', padx=5, pady=10)

        self.filter_entry = tk.Entry(self.header_frame)
        self.filter_entry.pack(side='left', padx=5, pady=10)
        self.filter_entry.bind("<KeyRelease>", lambda event: self.apply_filter())

        self.rename_headers_label = self.create_clickable_label(
            parent=self.header_frame,
            text="Rename Headers",
            command=self.rename_headers,
            allow_rename=False,
            is_new_table=False
        )
        self.rename_headers_label.pack(side='left', padx=5, pady=10, fill='x')

        self.table_frame = tk.Frame(self.main_frame, bg=BACKROUND_COLOR)
        self.table_frame.pack(expand=True, fill='both')

        self.new_table_label = self.create_clickable_label(
            parent=self.sidebar,
            text="Create New Table",
            command=self.create_new_table,
            allow_rename=False,
            is_new_table=True
        )
        self.new_table_label.pack(padx=0, pady=10, fill='x')

        self.table_labels_frame = tk.Frame(self.sidebar, bg=SIDEBAR_COLOR)
        self.table_labels_frame.pack(expand=True, fill='both', padx=0, pady=10)
        

        self.save_file_dir = Path("tables")
        self.tables = {}
        self.table_labels = {}
        self.original_data = {}
        self.headers = {}
        self.current_table_name = None
        self.rename_entry = None
        self.selected_table_label = None

        if not self.save_file_dir.exists():
            self.save_file_dir.mkdir()
        self.load_tables()

        self.add_custom_hover(self.new_table_label)

    def create_clickable_label(self, parent, text, command, allow_rename=True, is_new_table=False):
        """Create a clickable Label widget."""
        bg_color = BUTTON_COLOR

        label = tk.Label(
            parent,
            text=text,
            bg=bg_color,
            fg=BUTTON_TEXT_COLOR,
            borderwidth=0,
            relief='flat',
            cursor='arrow',
            anchor='w',
            padx=10,
            pady=5
        )

        label.bind("<Button-1>", lambda e: command())

        if allow_rename and not is_new_table:
            label.bind("<Double-1>", lambda e: command(rename=True))

        return label

    def create_new_table(self):
        """Create a new table and add it to the available tables list."""
        table_name = f"Table {len(self.tables) + 1}"
        self.add_table(table_name)
        self.load_selected_table(table_name)

    def add_table(self, table_name, data=None):
        """Add a table to the UI and internal storage."""
        default_data = [["" for _ in range(5)] for _ in range(10)]
        self.tables[table_name] = Sheet(
            self.table_frame,
            data=data if data else default_data
        )
        self.tables[table_name].enable_bindings()
        self.tables[table_name].extra_bindings([
            ("end_edit_cell", lambda event, table_name=table_name: self.save_data(table_name)),
        ])
        self.tables[table_name].pack_forget()
        self.original_data[table_name] = self.tables[table_name].get_sheet_data()
        self.headers[table_name] = self.tables[table_name].headers()

        table_label = self.create_clickable_label(
            parent=self.table_labels_frame,
            text=table_name,
            command=lambda name=table_name, rename=False: self.load_selected_table(name) if not rename else self.begin_rename(name),
            allow_rename=True,
            is_new_table=False
        )
        table_label.pack(fill='x', pady=2)
        self.table_labels[table_name] = table_label

        self.add_custom_hover(table_label)

    def load_tables(self):
        """Load existing tables from the directory."""
        for file_path in self.save_file_dir.glob("*.json"):
            table_name = file_path.stem
            with file_path.open('r') as file:
                data = json.load(file)
                headers = data.pop(0)
                self.add_table(table_name, data)
                self.headers[table_name] = headers

    def load_selected_table(self, table_name):
        """Display the selected table and hide others."""
        for name, sheet in self.tables.items():
            if name == table_name:
                sheet.pack(expand=True, fill='both')
                self.current_table_name = table_name
                sheet.headers(self.headers[name])
                self.apply_filter()

                selected_label = self.table_labels[name]
                selected_label.config(bg=BUTTON_COLOR_SELECTED)
                self.selected_table_label = selected_label
            else:
                sheet.pack_forget()

                label = self.table_labels[name]
                label.config(bg=BUTTON_COLOR)

    def save_data(self, table_name):
        """Save current data of a table to a file."""
        data = [self.headers[table_name]] + self.tables[table_name].get_sheet_data()
        file_path = self.save_file_dir / f"{table_name}.json"
        with file_path.open('w') as file:
            json.dump(data, file)
        print(f"Data for {table_name} saved successfully.")

    def apply_filter(self):
        """Apply filtering based on the entered term."""
        if not self.current_table_name:
            return

        term = self.filter_entry.get().lower()
        original_data = self.original_data[self.current_table_name]

        filtered_data = [
            row for row in original_data if any(term in str(cell).lower() for cell in row)
        ]

        self.tables[self.current_table_name].set_sheet_data(filtered_data)

    def rename_headers(self, rename=False):
        """Prompt user to rename headers of the currently selected table."""
        if not self.current_table_name:
            return

        if not rename:
            return

        current_headers = self.headers[self.current_table_name]
        new_headers = simpledialog.askstring(
            "Rename Headers",
            "Enter new headers (comma-separated):",
            initialvalue=", ".join(current_headers)
        )

        if new_headers:
            header_list = [header.strip() for header in new_headers.split(',')]
            self.headers[self.current_table_name] = header_list
            self.tables[self.current_table_name].headers(header_list)
            self.save_data(self.current_table_name)  # Automatically save the new headers

    def begin_rename(self, table_name):
        """Begin renaming of the table with a double click."""
        label = self.table_labels[table_name]
        
        if self.rename_entry:
            self.rename_entry.destroy()
        
        label.update_idletasks()
        label_x = label.winfo_x()
        label_y = label.winfo_y()
        label_width = label.winfo_width()
        label_height = label.winfo_height()
        
        self.rename_entry = tk.Entry(
            self.table_labels_frame,
            bg=label.cget("bg"),
            fg=BUTTON_TEXT_COLOR,
            font=label.cget("font"),
            relief='flat',
            borderwidth=0,
            highlightthickness=0,
            justify='left'
        )
        self.rename_entry.insert(0, table_name)
        
        self.rename_entry.place(x=label_x, y=label_y, width=label_width, height=label_height)
        self.rename_entry.focus()
        self.rename_entry.select_range(0, tk.END)
        
        self.rename_entry.bind('<Return>', lambda event: self.end_rename(table_name))
        self.rename_entry.bind('<FocusOut>', lambda event: self.cancel_rename())

    def end_rename(self, old_name):
        """Finish the renaming process for the specified table."""
        new_name = self.rename_entry.get().strip()
        
        if not new_name:
            messagebox.showerror("Invalid Name", "Table name cannot be empty.")
            self.rename_entry.focus_set()
            return

        if new_name in self.tables:
            messagebox.showerror("Duplicate Name", f"A table named '{new_name}' already exists.")
            self.rename_entry.focus_set()
            return

        if new_name != old_name and new_name not in self.tables:
            self.tables[new_name] = self.tables.pop(old_name)
            self.original_data[new_name] = self.original_data.pop(old_name)
            self.headers[new_name] = self.headers.pop(old_name)
        
            self.table_labels[new_name] = self.table_labels.pop(old_name)
            self.table_labels[new_name].config(text=new_name)
            
            if self.selected_table_label == self.table_labels[new_name]:
                self.selected_table_label = self.table_labels[new_name]
                self.table_labels[new_name].config(bg=BUTTON_COLOR_SELECTED)
            
            current_file = self.save_file_dir / f"{old_name}.json"
            new_file = self.save_file_dir / f"{new_name}.json"
        
            if current_file.exists():
                current_file.rename(new_file)
        
        self.rename_entry.destroy()
        self.rename_entry = None

    def cancel_rename(self):
        """Cancel the renaming entry."""
        if self.rename_entry:
            self.rename_entry.destroy()
            self.rename_entry = None

    def add_custom_hover(self, label):
        """Add custom hover effects to a label."""
        def on_enter(e):
            if label != self.selected_table_label:
                label.config(bg=BUTTON_COLOR_SELECTED)

        def on_leave(e):
            if label != self.selected_table_label:
                label.config(bg=BUTTON_COLOR)

        label.bind("<Enter>", on_enter)
        label.bind("<Leave>", on_leave)

if __name__ == "__main__":
    set_dpi_awareness()
    root = tk.Tk()
    app = TableApp(root)
    root.mainloop()