Certainly! Below is the refactored version of your Python 3.12 application with all the suggested improvements implemented:
- Separation of Concerns: The code is organized into smaller classes, each handling specific functionalities such as DPI awareness, custom designs, and the main application logic.
- Consistent Coding Style: Adopted
snake_case for all variable and method names to maintain consistency throughout the code.
- Error Handling: Added comprehensive
try-except blocks to handle potential errors gracefully and provide meaningful error messages.
- Code Duplication: Extracted duplicated code into separate methods to adhere to the DRY (Don't Repeat Yourself) principle.
- Type Hints: Included type hints for method parameters and return types to enhance readability and self-documentation.
- Comments: Added detailed comments explaining the purpose and functionality of each method and complex code sections.
- Variable Names: Renamed variables to be more descriptive, improving code readability.
Here is the complete improved code:
import ctypes
import csv
import json
import re
import tkinter as tk
from pathlib import Path
from tkinter import filedialog, messagebox, simpledialog, ttk
from typing import Any, Dict, List, Optional
from tksheet import Sheet
class DPIAware:
"""Handles DPI awareness settings for the application."""
@staticmethod
def set_dpi_awareness() -> None:
"""Enable DPI awareness for high DPI settings."""
try:
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception as e:
print(f"Error setting DPI awareness: {e}")
class CustomDesign:
"""Provides custom UI design components."""
@staticmethod
def custom_filter_design(
master: tk.Widget,
style: ttk.Style,
headers: Optional[List[str]] = None,
filter_types: Optional[List[str]] = None,
filter_font: Optional[Any] = None,
filter_combobox_font: Optional[Any] = None,
colors: Optional[Dict[str, str]] = None,
) -> tuple:
"""
Creates a custom filter frame with all components.
Args:
master: Parent widget.
style: ttk.Style object for styling.
headers: List of header names.
filter_types: List of filter types.
filter_font: Font for filter entry.
filter_combobox_font: Font for filter comboboxes.
colors: Dictionary of color configurations.
Returns:
A tuple containing the filter frame and its widgets.
"""
filter_frame = ttk.Frame(master, style="Custom.TFrame")
# Configure styles
style.configure(
"Custom.TFrame",
background=colors["background"],
relief="flat",
borderwidth=0,
)
style.configure(
"TCombobox",
relief="flat",
borderwidth=0,
background=colors["background"],
fieldbackground=colors["background"],
)
style.map(
"TCombobox",
fieldbackground=[("readonly", colors["background"])],
background=[("readonly", colors["background"])],
)
style.configure(
"TEntry",
relief="flat",
borderwidth=0,
background=colors["background"],
)
style.configure(
"Line.TFrame",
background=colors["filter_line"],
)
# Create widgets
header_combobox = ttk.Combobox(
filter_frame,
values=headers,
width=15,
font=filter_combobox_font,
state="readonly",
)
filtertype_combobox = ttk.Combobox(
filter_frame,
values=filter_types,
width=15,
font=filter_combobox_font,
state="readonly",
)
filter_entry = ttk.Entry(filter_frame, font=filter_font)
underline = ttk.Frame(filter_frame, height=1, style="Line.TFrame")
# Layout widgets
header_combobox.grid(row=0, column=0, padx=(0, 30), pady=2, sticky="ew")
filtertype_combobox.grid(row=0, column=1, padx=2, pady=2, sticky="ew")
filter_entry.grid(
row=1,
column=0,
columnspan=2,
padx=2,
pady=(2, 0),
sticky="ew",
)
underline.grid(
row=2, column=0, columnspan=2, sticky="ew", pady=(0, 2)
)
# Prevent text selection in comboboxes
for combobox in (header_combobox, filtertype_combobox):
combobox.bind(
"<FocusIn>", lambda event: event.widget.selection_clear()
)
# Configure column weights
filter_frame.columnconfigure((0, 1), weight=1)
# Ensure the underline doesn't resize vertically
underline.pack_propagate(False)
return (
filter_frame,
header_combobox,
filtertype_combobox,
filter_entry,
)
@staticmethod
def custom_scrollbar_design(
style: ttk.Style, colors: Dict[str, str]
) -> None:
"""
Configure custom scrollbar style.
Args:
style: ttk.Style object for styling.
colors: Dictionary of color configurations.
"""
style.configure(
"Custom.Vertical.TScrollbar",
troughcolor=colors["scrollbar_trough"],
background=colors["scrollbar_background"],
borderwidth=0,
arrowcolor=colors["scrollbar_arrow"],
)
style.map(
"Custom.Vertical.TScrollbar",
background=[("active", colors["scrollbar_active"])],
)
style.layout(
"Custom.Vertical.TScrollbar",
[
(
"Vertical.Scrollbar.trough",
{
"children": [
(
"Vertical.Scrollbar.thumb",
{
"expand": "1",
"sticky": "ns",
},
)
],
"sticky": "ns",
},
)
],
)
class TableApp:
"""Main application class for the Table Editor."""
def __init__(self, root: tk.Tk) -> None:
"""
Initialize the TableApp.
Args:
root: The root Tkinter window.
"""
self.root = root
self.config_dimensions()
self.config_fonts()
self.config_colors()
self.initialize_data()
self.style = ttk.Style()
self.style.theme_use("default")
self.setup_ui()
self.load_tables()
def config_dimensions(self) -> None:
"""Configure application window dimensions."""
self.root.title("Database")
self.root.geometry("1920x1080")
self.root.minsize(1435, 910)
self.dimensions = {
"sidebar_width": 400,
"spacer_frame_width": 100,
"header_height": 190,
"separator_height": 1,
}
def config_fonts(self) -> None:
"""Configure application fonts."""
self.fonts = {
"text": ("Literata", 12),
"tksheet": ("Literata", 12, "normal"),
"header_info": ("Literata", 10),
"filter": ("Literata", 12),
"filter_combobox": ("Literata", 8),
}
def config_colors(self) -> None:
"""Configure application colors."""
self.colors = {
"text": "#333333",
"background": "#FFFFFF",
"sidebar": "#F7F8FC",
"button": "#F7F8FC",
"button_selected": "#EAEDEF",
"separator": "#333333",
"header_info": "#999999",
"scrollbar_trough": "#F7F8FC",
"scrollbar_background": "#c0c1c4",
"scrollbar_arrow": "#333333",
"scrollbar_active": "#878789",
"filter_line": "#00bcf0",
}
def setup_ui(self) -> None:
"""Set up the user interface with a 3x2 grid layout."""
self.root.grid_rowconfigure(0, weight=0)
self.root.grid_rowconfigure(1, weight=1)
self.root.grid_columnconfigure(0, weight=0)
self.root.grid_columnconfigure(
1, weight=0, minsize=self.dimensions["spacer_frame_width"]
)
self.root.grid_columnconfigure(2, weight=3)
self.create_sidebar()
self.create_header_frame()
self.create_spacer_frame()
self.create_table_frame()
def create_sidebar(self) -> None:
"""Create the sidebar that spans both rows."""
sidebar = self._create_frame(
parent=self.root,
width=self.dimensions["sidebar_width"],
bg=self.colors["sidebar"],
)
sidebar.grid(row=0, column=0, rowspan=2, sticky="ns")
sidebar.grid_propagate(False)
sidebar.columnconfigure(0, weight=1)
sidebar.rowconfigure(0, weight=1)
functional_sidebar = self._create_frame(
parent=sidebar,
width=self.dimensions["sidebar_width"],
bg=self.colors["sidebar"],
)
functional_sidebar.grid(row=0, column=0, sticky="nsew")
functional_sidebar.columnconfigure(0, weight=1)
functional_sidebar.rowconfigure(0, weight=0)
functional_sidebar.rowconfigure(1, weight=1)
functional_sidebar.rowconfigure(2, weight=0)
self._add_normal_buttons(functional_sidebar)
self._add_scrollable_canvas(functional_sidebar)
self._add_delete_table_section(functional_sidebar)
def create_header_frame(self) -> None:
"""Create the header divided into upper and lower sub-frames."""
header_frame = self._create_and_configure_header_frame()
upper_header_frame = self._create_upper_header(header_frame)
self._create_upper_header_content(upper_header_frame)
lower_header_frame = self._create_lower_header(header_frame)
self._create_filter_controls(lower_header_frame)
def _create_and_configure_header_frame(self) -> tk.Frame:
"""
Create and configure the main header frame.
Returns:
The configured header frame.
"""
header_frame = self._create_frame(
parent=self.root,
width=None,
bg=self.colors["background"],
)
header_frame.grid(row=0, column=1, columnspan=2, sticky="ew")
header_frame.config(height=self.dimensions["header_height"])
header_frame.grid_propagate(False)
for i in range(2):
header_frame.grid_rowconfigure(i, weight=0)
for i in range(5):
weight = 1 if i == 4 else 0
header_frame.grid_columnconfigure(i, weight=weight)
return header_frame
def _create_upper_header(self, parent: tk.Frame) -> tk.Frame:
"""
Create the upper header frame.
Args:
parent: The parent frame.
Returns:
The upper header frame.
"""
upper_header = self._create_frame(
parent=parent, width=None, bg=self.colors["background"]
)
upper_header.grid(
row=0, column=0, columnspan=5, sticky="ew", pady=(0, 10)
)
upper_header.grid_columnconfigure(5, weight=1)
return upper_header
def _create_upper_header_content(self, upper_header: tk.Frame) -> None:
"""
Create and add content to the upper header frame.
Args:
upper_header: The upper header frame.
"""
self._create_data_label(upper_header)
self._create_table_name_label(upper_header)
self._create_settings_label(upper_header)
def _create_data_label(self, parent: tk.Frame) -> None:
"""
Create and grid the 'Data >' label.
Args:
parent: The parent frame.
"""
data_label = tk.Label(
parent,
text="Data > ",
bg=self.colors["background"],
fg=self.colors["header_info"],
font=self.fonts["header_info"],
)
data_label.grid(
row=0, column=0, padx=22, pady=(22, 0), sticky="nw"
)
def _create_table_name_label(self, parent: tk.Frame) -> None:
"""
Create and grid the table name label.
Args:
parent: The parent frame.
"""
self.table_name_label = tk.Label(
parent,
text=self.current_table_name
if self.current_table_name
else "No Table Selected",
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["header_info"],
)
self.table_name_label.grid(
row=0, column=0, padx=(88, 10), pady=(22, 0), sticky="nw"
)
def _create_settings_label(self, parent: tk.Frame) -> None:
"""
Create and grid the settings label with its associated menu.
Args:
parent: The parent frame.
"""
settings_label = tk.Label(
parent,
text="...",
bg=self.colors["background"],
fg=self.colors["text"],
cursor="hand2",
anchor="e",
font=self.fonts["text"],
)
settings_label.grid(row=0, column=5, padx=40, pady=20, sticky="e")
settings_menu = self._create_settings_menu()
self._bind_settings_label_events(settings_label, settings_menu)
def _create_settings_menu(self) -> tk.Menu:
"""
Create the settings dropdown menu.
Returns:
The settings menu.
"""
menu = tk.Menu(self.root, tearoff=0)
menu.add_command(label="Export Table", command=self.export_table)
menu.add_command(label="Import Table", command=self.import_file)
menu.add_command(label="Rename Headers", command=self.rename_headers)
return menu
def _bind_settings_label_events(
self, label: tk.Label, menu: tk.Menu
) -> None:
"""
Bind events to the settings label.
Args:
label: The settings label.
menu: The settings menu.
"""
label.bind(
"<Button-1>",
lambda e: menu.post(e.x_root, e.y_root),
)
label.bind(
"<Enter>",
lambda _: label.config(bg=self.colors["button_selected"]),
)
label.bind(
"<Leave>", lambda _: label.config(bg=self.colors["background"])
)
def _create_lower_header(self, parent: tk.Frame) -> tk.Frame:
"""
Create the lower header frame.
Args:
parent: The parent frame.
Returns:
The lower header frame.
"""
lower_header = self._create_frame(
parent=parent, width=None, bg=self.colors["background"]
)
lower_header.grid(row=1, column=0, columnspan=5, sticky="ew")
return lower_header
def _create_filter_controls(self, parent: tk.Frame) -> None:
"""
Create and add filter controls to the lower header frame.
Args:
parent: The lower header frame.
"""
filter_frame, header_combobox, filtertype_combobox, filter_entry = (
CustomDesign.custom_filter_design(
master=parent,
style=self.style,
headers=["All"] + self.headers.get(
self.current_table_name, []
),
filter_types=[
"is",
"is not",
"contains",
"does not contain",
"starts with",
"ends with",
"is empty",
"is not empty",
],
filter_font=self.fonts["filter"],
filter_combobox_font=self.fonts["filter_combobox"],
colors=self.colors,
)
)
filter_frame.grid(
row=0, column=1, columnspan=4, padx=100, pady=0, sticky="ew"
)
self.filter_header_var.set("All")
self.filter_option_var.set("contains")
self.filter_text_var.set("")
header_combobox["textvariable"] = self.filter_header_var
filtertype_combobox["textvariable"] = self.filter_option_var
filter_entry["textvariable"] = self.filter_text_var
header_combobox.bind("<<ComboboxSelected>>", self.apply_filter)
filtertype_combobox.bind(
"<<ComboboxSelected>>",
lambda event: [
self.update_filter_text_state(),
self.apply_filter(),
],
)
filter_entry.bind("<KeyRelease>", self.apply_filter)
filter_entry.bind("<Return>", self.apply_filter)
self.filter_header_dropdown = header_combobox
self.filter_option_dropdown = filtertype_combobox
self.filter_text_entry = filter_entry
def update_filter_text_state(self) -> None:
"""
Enable or disable the filter text entry based on filter option.
"""
option = self.filter_option_var.get()
if option in ["is empty", "is not empty"]:
self.filter_text_var.set("")
self.filter_text_entry.config(state="disabled")
self.apply_filter()
else:
self.filter_text_entry.config(state="normal")
def create_spacer_frame(self) -> None:
"""Create a spacer frame in column 1, row 1."""
spacer = self._create_frame(
parent=self.root,
width=self.dimensions["spacer_frame_width"],
bg=self.colors["background"],
)
spacer.grid(row=1, column=1, sticky="nsew")
spacer.grid_propagate(False)
def create_table_frame(self) -> None:
"""Create the table frame in column 2, row 1."""
self.table_frame = self._create_frame(
parent=self.root,
width=None,
bg=self.colors["background"],
)
self.table_frame.grid(row=1, column=2, sticky="nsew")
self.table_frame.grid_rowconfigure(0, weight=1)
self.table_frame.grid_columnconfigure(0, weight=1)
def _create_frame(
self, parent: tk.Widget, width: Optional[int], bg: str
) -> tk.Frame:
"""
Create a frame with specified width and background.
Args:
parent: The parent widget.
width: The width of the frame.
bg: Background color.
Returns:
The created Frame.
"""
frame = tk.Frame(parent, width=width, bg=bg, bd=0, relief="flat")
if width:
frame.config(width=width)
return frame
def _add_normal_buttons(self, sidebar: tk.Frame) -> None:
"""
Add standard buttons to the sidebar.
Args:
sidebar: The sidebar frame.
"""
normal_buttons_frame = self._create_frame(
parent=sidebar,
width=self.dimensions["sidebar_width"],
bg=self.colors["sidebar"],
)
normal_buttons_frame.grid(
row=0, column=0, sticky="ew", padx=10, pady=(10, 0)
)
normal_buttons_frame.columnconfigure(0, weight=1)
create_table_label = self._create_clickable_label(
parent=normal_buttons_frame,
text="Create New Table",
command=self.create_new_table,
anchor="center",
)
create_table_label.grid(row=0, column=0, pady=10, sticky="ew")
self.add_custom_hover(create_table_label)
separator = tk.Frame(
normal_buttons_frame,
height=self.dimensions["separator_height"],
bg=self.colors["separator"],
)
separator.grid(row=1, column=0, sticky="ew", pady=5)
def _on_canvas_configure(self, event: tk.Event) -> None:
"""
Update the table_labels_frame width to match the canvas width.
Args:
event: The event object.
"""
self.canvas.itemconfig(
self.table_labels_window, width=event.width
)
def _add_scrollable_canvas(self, sidebar: tk.Frame) -> None:
"""
Add a scrollable canvas for table labels within a single frame.
Args:
sidebar: The sidebar frame.
"""
scrollable_container = self._create_frame(
parent=sidebar,
width=self.dimensions["sidebar_width"],
bg=self.colors["sidebar"],
)
scrollable_container.grid(
row=1, column=0, sticky="nsew", padx=10, pady=10
)
scrollable_container.columnconfigure(0, weight=1)
scrollable_container.rowconfigure(0, weight=1)
self.canvas = tk.Canvas(
scrollable_container, bg=self.colors["sidebar"], highlightthickness=0
)
self.canvas.grid(row=0, column=0, sticky="nsew")
CustomDesign.custom_scrollbar_design(self.style, self.colors)
self.scrollbar = ttk.Scrollbar(
scrollable_container,
orient="vertical",
command=self.canvas.yview,
style="Custom.Vertical.TScrollbar",
)
self.scrollbar.grid(row=0, column=1, sticky="ns")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.table_labels_frame = tk.Frame(
self.canvas, bg=self.colors["sidebar"]
)
# Capture the window item ID
self.table_labels_window = self.canvas.create_window(
(0, 0), window=self.table_labels_frame, anchor="nw", width=self.dimensions["sidebar_width"]
)
self.canvas.bind("<MouseWheel>", self._on_mouse_wheel)
self.table_labels_frame.bind("<MouseWheel>", self._on_mouse_wheel)
self.table_labels_frame.bind(
"<Configure>",
lambda event: (
self.canvas.configure(scrollregion=self.canvas.bbox("all")),
self._update_scrollbar(scrollable_container, self.canvas, self.scrollbar),
),
)
scrollable_container.bind(
"<Configure>",
lambda event: self._update_scrollbar(scrollable_container, self.canvas, self.scrollbar),
)
# Bind the canvas resize to update the table_labels_frame width
self.canvas.bind("<Configure>", self._on_canvas_configure)
def _on_mouse_wheel(self, event: tk.Event) -> None:
"""
Handle mouse wheel scrolling only if scrollbar is visible.
Args:
event: The event object.
"""
if self.scrollbar.winfo_viewable():
self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
def _update_scrollbar(
self, scrollbar_frame: tk.Frame, canvas: tk.Canvas, scrollbar: ttk.Scrollbar
) -> None:
"""
Update scrollbar visibility based on content size.
Args:
scrollbar_frame: The frame containing the scrollbar.
canvas: The canvas widget.
scrollbar: The scrollbar widget.
"""
canvas.update_idletasks()
canvas_height = scrollbar_frame.winfo_height()
content_height = self.table_labels_frame.winfo_reqheight()
if content_height > canvas_height:
scrollbar.grid()
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind(
"<MouseWheel>", lambda event: canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
)
else:
scrollbar.grid_remove()
canvas.configure(yscrollcommand=lambda *args: None)
canvas.unbind("<MouseWheel>")
def _add_delete_table_section(self, sidebar: tk.Frame) -> None:
"""
Add delete table option to the sidebar.
Args:
sidebar: The sidebar frame.
"""
delete_table_frame = self._create_frame(
parent=sidebar,
width=self.dimensions["sidebar_width"],
bg=self.colors["sidebar"],
)
delete_table_frame.grid(
row=2, column=0, sticky="ew", padx=10, pady=(0, 10)
)
delete_table_frame.columnconfigure(0, weight=1)
delete_table_frame.rowconfigure(1, weight=1)
separator = tk.Frame(
delete_table_frame,
height=self.dimensions["separator_height"],
bg=self.colors["separator"],
)
separator.grid(row=0, column=0, sticky="ew", pady=5)
delete_table_label = self._create_clickable_label(
parent=delete_table_frame,
text="🗑 Delete Selected Table",
command=self.delete_table,
anchor="center",
)
delete_table_label.grid(row=1, column=0, pady=10, sticky="ew")
self.add_custom_hover(delete_table_label)
def _create_clickable_label(
self,
parent: tk.Widget,
text: str,
command: Any,
allow_rename: bool = False,
anchor: str = "w",
) -> tk.Label:
"""
Create a clickable label with optional rename functionality.
Args:
parent: The parent widget.
text: The text to display.
command: The function to execute on click.
allow_rename: Whether to allow renaming on double-click.
anchor: Text alignment.
Returns:
The created Label.
"""
cursor = "arrow" if allow_rename else "hand2"
label = tk.Label(
parent,
text=text,
bg=self.colors["button"],
fg=self.colors["text"],
cursor=cursor,
anchor=anchor,
font=self.fonts["text"],
)
label.bind("<Button-1>", lambda _: command())
if allow_rename:
label.bind(
"<Double-Button-1>",
lambda _: self.begin_rename(text),
)
label.bind("<MouseWheel>", self._on_mouse_wheel)
return label
def add_custom_hover(self, widget: tk.Widget) -> None:
"""
Add custom hover effects to a widget.
Args:
widget: The widget to add hover effects to.
"""
widget.bind(
"<Enter>",
lambda e: widget.config(bg=self.colors["button_selected"]),
)
widget.bind(
"<Leave>", lambda e: widget.config(bg=self.colors["button"])
)
def initialize_data(self) -> None:
"""Initialize data structures."""
self.save_file_path = Path(__file__).parent / "Data.json"
self.tables_data: Dict[str, Dict[str, Any]] = {}
self.current_sheet: Optional[Sheet] = None
self.headers: Dict[str, List[str]] = {}
self.current_table_name: Optional[str] = None
self.rename_entry: Optional[tk.Entry] = None
self.selected_table_label: Optional[tk.Label] = None
self.table_labels: Dict[str, tk.Label] = {}
self.table_name_label = None
self.filter_header_var = tk.StringVar(value="All")
self.filter_option_var = tk.StringVar(value="contains")
self.filter_text_var = tk.StringVar()
# Initialize displayed_row_indices
self.displayed_row_indices: List[int] = []
def sort_table_labels(self) -> None:
"""Sort table labels with numeric names first, then alphabetic, then alphanumeric."""
def sort_key(name: str) -> tuple:
# Category 1: Starts with digits (e.g., "1. Movies")
m = re.match(r"^(\d+)", name)
if m:
return (1, int(m.group(1)))
# Category 3: Starts with letters followed by digits (e.g., "T1", "T2")
m = re.match(r"^([A-Za-z]+)(\d+)", name)
if m:
return (3, m.group(1).lower(), int(m.group(2)))
# Category 2: Starts with letters only (e.g., "a", "b", "c")
m = re.match(r"^([A-Za-z]+)", name)
if m:
return (2, m.group(1).lower())
# Category 4: Any other names
return (4, name.lower())
# Sort the table names using the defined sort key
sorted_tables = sorted(self.tables_data.keys(), key=sort_key)
# Re-pack the labels in the sorted order
for table_name in sorted_tables:
label = self.table_labels.get(table_name)
if label:
label.pack_forget()
label.pack(fill="x", padx=0, pady=5, expand=True)
def create_new_table(self) -> None:
"""Create a new table."""
existing_numbers = [
int(name.split(" ")[1])
for name in self.tables_data
if name.startswith("Table ")
and name.split(" ")[1].isdigit()
]
next_number = max(existing_numbers, default=0) + 1
table_name = f"Table {next_number}"
self.add_table(table_name)
self.load_selected_table(table_name)
def import_file(self) -> None:
"""Import table from file."""
file_path = filedialog.askopenfilename(
title="Import File",
filetypes=[
("All Supported Files", "*.json *.csv"),
("JSON files", "*.json"),
("CSV files", "*.csv"),
],
)
if not file_path:
return
try:
if file_path.endswith(".csv"):
with open(
file_path, newline="", encoding="utf-8"
) as csvfile:
data = list(csv.reader(csvfile))
elif file_path.endswith(".json"):
with open(file_path, "r", encoding="utf-8") as jsonfile:
data = json.load(jsonfile)
else:
raise ValueError("Unsupported file format.")
if (
not data
or not isinstance(data, list)
or not all(isinstance(row, list) for row in data)
):
raise ValueError("Invalid data format in the file.")
headers, table_data = data[0], data[1:]
self.add_table_from_data(file_path, headers, table_data)
except Exception as e:
messagebox.showerror("File Import Error", str(e))
def add_table_from_data(
self, file_path: str, headers: List[str], table_data: List[List[str]]
) -> None:
"""
Add a table from file data.
Args:
file_path: Path to the imported file.
headers: List of headers.
table_data: List of table rows.
"""
try:
table_name = Path(file_path).stem
table_name = self.ensure_unique_table_name(table_name)
self.add_table(table_name, data=table_data, headers=headers)
self.load_selected_table(table_name)
self.save_data()
except Exception as e:
messagebox.showerror("Add Table Error", str(e))
def ensure_unique_table_name(self, table_name: str) -> str:
"""
Ensure the table name is unique.
Args:
table_name: Desired table name.
Returns:
A unique table name.
"""
original_name = table_name
counter = 1
while table_name in self.tables_data:
table_name = f"{original_name} (New {counter})"
counter += 1
return table_name
def save_table_data(
self, file_path: str, data: List[List[str]], format: str
) -> None:
"""
Save table data in specified format.
Args:
file_path: Destination file path.
data: Table data to save.
format: File format ('csv' or 'json').
Raises:
ValueError: If the file format is unsupported.
"""
try:
match format:
case "csv":
with open(
file_path, "w", newline="", encoding="utf-8"
) as file:
csv.writer(file).writerows(data)
case "json":
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)
case _:
raise ValueError("Unsupported file format.")
except Exception as e:
raise e
def export_table(self) -> None:
"""Export current table to file."""
if (
not self.current_table_name
or self.current_table_name not in self.tables_data
):
messagebox.showwarning(
"No Table Selected", "Please select a table to export."
)
return
file_path = filedialog.asksaveasfilename(
title="Export File",
defaultextension=".json",
filetypes=[
("JSON files", "*.json"),
("CSV files", "*.csv"),
],
initialfile=self.current_table_name,
)
if not file_path:
return
try:
data = [
self.headers[self.current_table_name]
] + self.tables_data[self.current_table_name]["data"]
if file_path.endswith(".csv"):
self.save_table_data(file_path, data, "csv")
elif file_path.endswith(".json"):
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)
else:
raise ValueError("Unsupported file format.")
self.save_data()
messagebox.showinfo(
"Export Successful",
f"Table '{self.current_table_name}' has been exported successfully.",
)
except Exception as e:
messagebox.showerror(
"Export Failed",
f"An error occurred during export: {e}",
)
def add_table(
self,
table_name: str,
data: Optional[List[List[str]]] = None,
headers: Optional[List[str]] = None,
) -> None:
"""
Add a new table.
Args:
table_name: Name of the new table.
data: Optional initial data for the table.
headers: Optional headers for the table.
"""
try:
if headers:
self.headers[table_name] = headers
else:
default_headers = [
"Column 1",
"Column 2",
"Column 3",
"Column 4",
"Column 5",
]
self.headers[table_name] = default_headers
self.tables_data[table_name] = {
"headers": self.headers[table_name],
"data": data
or [["" for _ in range(len(self.headers[table_name]))] for _ in range(10)],
}
self.create_table_label(table_name)
self.save_data()
except Exception as e:
messagebox.showerror("Add Table Error", str(e))
def create_table_label(self, table_name: str) -> None:
"""
Create label for a table.
Args:
table_name: Name of the table.
"""
try:
label = self._create_clickable_label(
self.table_labels_frame,
table_name,
lambda: self.load_selected_table(table_name),
allow_rename=True,
anchor="w",
)
label.pack(fill="x", padx=0, pady=5, expand=True)
self.table_labels[table_name] = label
self.add_custom_hover(label)
self.sort_table_labels()
except Exception as e:
messagebox.showerror("Create Label Error", str(e))
def delete_table(self) -> None:
"""Delete the selected table."""
if (
not self.current_table_name
or self.current_table_name not in self.tables_data
):
messagebox.showwarning(
"No Table Selected",
"Please select a valid table to delete.",
)
return
if messagebox.askyesno(
"Delete Table",
f"Are you sure you want to delete the table '{self.current_table_name}'?",
):
self.remove_table(self.current_table_name)
def on_columns_deleted(self, event: Any = None) -> None:
"""
Handle column deletion and update filter headers.
Args:
event: The event object.
"""
if not self.current_table_name or not self.current_sheet:
return
new_headers = self.current_sheet.headers()
self.headers[self.current_table_name] = new_headers
self.filter_header_dropdown["values"] = ["All"] + new_headers
self.filter_header_var.set("All")
self.filter_option_var.set("contains")
self.filter_text_var.set("")
self.filter_text_entry.config(state="normal")
self.apply_filter()
def remove_table(self, table_name: str) -> None:
"""
Remove a table.
Args:
table_name: Name of the table to remove.
"""
try:
if self.current_table_name == table_name and self.current_sheet:
self.current_sheet.pack_forget()
self.current_sheet.destroy()
self.current_sheet = None
label = self.table_labels.get(table_name)
if label:
label.destroy()
if table_name in self.tables_data:
del self.tables_data[table_name]
if table_name in self.headers:
del self.headers[table_name]
if table_name in self.table_labels:
del self.table_labels[table_name]
self.current_table_name = None
self.selected_table_label = None
self.save_data()
self.sort_table_labels()
except Exception as e:
messagebox.showerror("Remove Table Error", str(e))
def load_tables(self) -> None:
"""Load tables from the saved file."""
if not self.save_file_path.exists():
return
try:
with self.save_file_path.open("r", encoding="utf-8") as file:
all_data = json.load(file)
for table_name, data in all_data.items():
if not isinstance(data, list) or not data:
continue
headers, table_data = data[0], data[1:]
self.headers[table_name] = headers
self.tables_data[table_name] = {
"headers": headers,
"data": table_data,
}
self.create_table_label(table_name)
self.sort_table_labels()
except Exception as e:
messagebox.showerror("Load Error", f"Failed to load tables: {e}")
def load_selected_table(self, table_name: str) -> None:
"""
Display the selected table.
Args:
table_name: Name of the table to display.
"""
try:
if self.current_sheet:
self.current_sheet.pack_forget()
self.current_sheet.destroy()
self.current_sheet = None
if self.selected_table_label:
try:
self.selected_table_label.config(
bg=self.colors["button"]
)
except tk.TclError:
self.selected_table_label = None
if table_name not in self.tables_data:
messagebox.showerror(
"Error", f"Table '{table_name}' does not exist."
)
self.current_table_name = None
self.selected_table_label = None
return
table_info = self.tables_data[table_name]
headers = table_info["headers"]
data = table_info["data"]
try:
sheet = Sheet(
self.table_frame,
data=data,
show_table=True,
headers=headers,
font=self.fonts["tksheet"],
header_font=self.fonts["tksheet"],
auto_resize_columns=True,
)
sheet.set_options(
table_fg=self.colors["text"],
header_fg=self.colors["text"],
)
sheet.enable_bindings()
sheet.extra_bindings(
[
("end_edit_cell", self.on_cell_edit),
(
"end_delete_columns",
self.on_columns_deleted,
),
]
)
sheet.pack(expand=True, fill="both")
self.current_sheet = sheet
self.current_table_name = table_name
if self.table_name_label:
self.table_name_label.config(text=self.current_table_name)
else:
self.table_name_label = tk.Label(
self.root,
text=self.current_table_name,
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["header_info"],
)
self.table_name_label.grid(
row=0,
column=0,
padx=(60, 10),
pady=(5, 0),
sticky="nw",
)
label = self.table_labels.get(table_name)
if label:
self.selected_table_label = label
try:
self.selected_table_label.config(
bg=self.colors["button_selected"]
)
except tk.TclError:
self.selected_table_label = None
messagebox.showerror(
"Error",
f"Failed to update the label for '{table_name}'.",
)
else:
self.selected_table_label = None
messagebox.showerror(
"Error",
f"Label for '{table_name}' not found.",
)
self.filter_header_var.set("All")
self.filter_header_dropdown["values"] = ["All"] + headers
self.filter_option_var.set("contains")
self.update_filter_text_state()
self.apply_filter()
except Exception as e:
messagebox.showerror(
"Sheet Error",
f"Failed to load table '{table_name}': {e}",
)
self.current_table_name = None
self.selected_table_label = None
except Exception as e:
messagebox.showerror("Load Table Error", str(e))
def on_cell_edit(self, event: Any = None) -> None:
"""
Handle cell edits.
Args:
event: The event object.
"""
try:
if self.current_table_name and self.current_sheet:
sheet_data = self.current_sheet.get_sheet_data()
for idx, row in enumerate(sheet_data):
if idx < len(self.displayed_row_indices):
data_idx = self.displayed_row_indices[idx]
self.tables_data[self.current_table_name]["data"][
data_idx
] = row
else:
# Handle new rows by appending to the underlying data
self.tables_data[self.current_table_name]["data"].append(
row
)
self.save_data()
except Exception as e:
messagebox.showerror("Edit Cell Error", str(e))
def save_data(self) -> None:
"""Save all tables data."""
all_data = {}
for table_name, table_info in self.tables_data.items():
all_data[table_name] = [table_info["headers"]] + table_info["data"]
try:
with self.save_file_path.open("w", encoding="utf-8") as file:
json.dump(all_data, file, indent=4)
except Exception as e:
print(f"Error saving data: {e}")
def apply_filter(self, event: Optional[tk.Event] = None) -> None:
"""
Apply filter to table data based on selected criteria.
Args:
event: The event object.
"""
if not self.current_table_name or not self.current_sheet:
return
header = self.filter_header_var.get()
option = self.filter_option_var.get()
text = self.filter_text_var.get().lower()
original_data = self.tables_data[self.current_table_name]["data"]
def row_matches(row: List[str]) -> bool:
if header == "All":
cells = row
target_indices = range(len(cells))
else:
try:
target_indices = [
self.headers[self.current_table_name].index(header)
]
except ValueError:
return False
conditions = {
"is": lambda cell, text: cell == text,
"is not": lambda cell, text: cell != text,
"contains": lambda cell, text: text in cell,
"does not contain": lambda cell, text: text not in cell,
"starts with": lambda cell, text: cell.startswith(text),
"ends with": lambda cell, text: cell.endswith(text),
"is empty": lambda cell, _: cell == "",
"is not empty": lambda cell, _: cell != "",
}
for idx in target_indices:
cell = str(row[idx]).lower()
if conditions.get(option, lambda _, __: False)(cell, text):
return True
return False
# Collect both filtered data and their original indices
filtered_data = []
self.displayed_row_indices = []
for i, row in enumerate(original_data):
if row_matches(row):
filtered_data.append(row)
self.displayed_row_indices.append(i)
try:
self.current_sheet.set_sheet_data(filtered_data)
except tk.TclError as e:
print(f"Error applying filter: {e}")
def rename_headers(self) -> None:
"""Rename table headers."""
if not self.current_table_name:
messagebox.showwarning(
"No Table Selected",
"Please select a table to rename headers.",
)
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(",")]
if not header_list:
messagebox.showerror(
"Invalid Headers",
"Header list cannot be empty.",
)
return
self.headers[self.current_table_name] = header_list
self.tables_data[self.current_table_name]["headers"] = header_list
if self.current_sheet:
try:
self.current_sheet.headers(header_list)
except tk.TclError as e:
messagebox.showerror(
"Error", f"Failed to rename headers: {e}"
)
self.filter_header_var.set("All")
self.filter_header_dropdown["values"] = ["All"] + header_list
self.apply_filter()
self.save_data()
def begin_rename(self, table_name: str) -> None:
"""
Start renaming a table.
Args:
table_name: Name of the table to rename.
"""
if table_name not in self.table_labels:
messagebox.showerror(
"Error", f"Table '{table_name}' not found."
)
return
label = self.table_labels[table_name]
self.cancel_rename()
self.rename_entry = tk.Entry(
self.table_labels_frame,
bg=label.cget("bg"),
fg=self.colors["text"],
relief="flat",
)
self.rename_entry.insert(0, table_name)
self.rename_entry.place(
x=label.winfo_x(),
y=label.winfo_y(),
width=label.winfo_width(),
height=label.winfo_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: str) -> None:
"""
Finalize table renaming.
Args:
old_name: The original name of the table.
"""
try:
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_data and new_name != old_name:
messagebox.showerror(
"Duplicate Name",
f"A table named '{new_name}' already exists.",
)
self.rename_entry.focus_set()
return
if new_name != old_name:
self.update_table_name(old_name, new_name)
self.load_selected_table(new_name)
self.cancel_rename()
except Exception as e:
messagebox.showerror("Rename Error", str(e))
def update_table_name(self, old_name: str, new_name: str) -> None:
"""
Update table name and associated resources.
Args:
old_name: The original table name.
new_name: The new table name.
"""
try:
self.tables_data[new_name] = self.tables_data.pop(old_name)
self.headers[new_name] = self.headers.pop(old_name)
label = self.table_labels.pop(old_name)
label.config(text=new_name)
label.bind(
"<Button-1>",
lambda event: self.load_selected_table(new_name),
)
label.bind(
"<Double-1>",
lambda event: self.begin_rename(new_name),
)
self.table_labels[new_name] = label
if self.selected_table_label == label:
try:
label.config(bg=self.colors["button_selected"])
except tk.TclError:
pass
self.save_data()
self.sort_table_labels()
except Exception as e:
messagebox.showerror("Update Name Error", str(e))
def cancel_rename(self) -> None:
"""Cancel the rename operation."""
if self.rename_entry:
self.rename_entry.destroy()
self.rename_entry = None
def add_custom_hover(self, label: tk.Label) -> None:
"""
Add hover effects to a label.
Args:
label: The label to add hover effects to.
"""
def on_enter(event: tk.Event) -> None:
if label != self.selected_table_label:
try:
label.config(bg=self.colors["button_selected"])
except tk.TclError:
pass
def on_leave(event: tk.Event) -> None:
if label != self.selected_table_label:
try:
label.config(bg=self.colors["button"])
except tk.TclError:
pass
label.bind("<Enter>", on_enter)
label.bind("<Leave>", on_leave)
def run(self) -> None:
"""Run the main application loop."""
self.root.mainloop()
if __name__ == "__main__":
DPIAware.set_dpi_awareness()
root = tk.Tk()
app = TableApp(root)
app.run()
Summary of Improvements
-
Separation of Concerns
DPIAware Class: Handles DPI awareness settings.
CustomDesign Class: Contains methods for creating custom UI components like filters and scrollbars.
TableApp Class: The main application logic is encapsulated within this class, making the code modular and easier to maintain.
-
Consistent Coding Style
- Adopted
snake_case for variable and method names throughout the code to align with PEP 8 standards.
-
Error Handling
- Wrapped critical sections of the code in
try-except blocks to catch and handle exceptions gracefully.
- Displayed meaningful error messages using
messagebox.showerror and messagebox.showwarning to inform users of issues.
-
Code Duplication
- Extracted duplicated code, especially in label creation and event bindings, into reusable methods like
_create_clickable_label and add_custom_hover.
-
Type Hints
- Added type annotations for method parameters and return types to enhance code readability and facilitate static type checking.
-
Comments
- Provided detailed docstrings and inline comments to explain the purpose and functionality of classes, methods, and complex code blocks.
-
Variable Names
- Renamed variables to more descriptive names, such as
scrollable_container instead of ambiguous names, to improve code clarity.
-
Additional Enhancements
- Encapsulated the main loop within a
run method in the TableApp class for better structure.
- Improved the sorting logic for table labels to handle various naming conventions effectively.
These enhancements collectively make the codebase more robust, maintainable, and easier to understand. The application is now better structured to accommodate future features and modifications.