Modfy the code so that the sidebar covers the whole mheight of the programm and does not have the header over it the header should move to the side a bit to make sapce for the sidebar. Python code: import sys
import sqlite3
from pathlib import Path
import tkinter as tk
from tkinter import ttk, messagebox, font, simpledialog
MEDIA_DB = Path(file).parent / 'media.db'
if sys.platform.startswith('win'):
try:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception as e:
print("Error setting DPI awareness:", e)
def run_query(query: str, parameters: tuple = ()) -> list:
"""Execute a SQL query and return the results if it's a SELECT statement."""
with sqlite3.connect(MEDIA_DB) as conn:
cursor = conn.cursor()
result = cursor.execute(query, parameters)
if query.strip().lower().startswith("select"):
return result.fetchall()
conn.commit()
def initialize_database() -> None:
"""Initialize the database with necessary metadata tables."""
run_query('''
CREATE TABLE IF NOT EXISTS tables (
name TEXT PRIMARY KEY
);
''')
run_query('''
CREATE TABLE IF NOT EXISTS fields (
id INTEGER PRIMARY KEY AUTOINCREMENT,
table_name TEXT,
field_name TEXT,
field_type TEXT,
is_combo BOOLEAN,
FOREIGN KEY(table_name) REFERENCES tables(name)
);
''')
run_query('''
CREATE TABLE IF NOT EXISTS field_options (
id INTEGER PRIMARY KEY AUTOINCREMENT,
table_name TEXT,
field_name TEXT,
option_value TEXT,
FOREIGN KEY(table_name) REFERENCES tables(name)
);
''')
class MediaTracker(tk.Tk):
"""A Tkinter application to track user-defined media databases."""
code
Assistant
def __init__(self):
super().__init__()
self.configure(bg='white')
self.title("Media Tracker")
self.geometry("1200x600")
self.custom_font = self._get_custom_font()
self.current_table = None
self.current_row_id = None
self.tree = None
self.scrollbar = None
self._configure_style()
self._configure_scrollbar_style()
self._configure_grid()
self._create_widgets()
self.load_tables()
def _get_custom_font(self) -> font.Font:
"""Retrieve the custom font, defaulting to Arial if Literata is unavailable."""
try:
return font.Font(family='Literata', size=12)
except:
return font.Font(family='Arial', size=12)
def _configure_style(self) -> None:
"""Configure the styles for Treeview and buttons."""
font_line_space = self.custom_font.metrics('linespace')
row_height = font_line_space + 4
style = ttk.Style()
style.theme_use("default")
style.layout(
'Custom.Treeview',
[('Custom.Treeview.treearea', {'sticky': 'nsew'})]
)
style.configure(
"Custom.Treeview",
font=self.custom_font,
rowheight=row_height,
borderwidth=0,
relief='flat',
highlightthickness=0,
background='white',
fieldbackground='white'
)
style.configure(
"Custom.Treeview.Heading",
font=(self.custom_font.cget("family"), self.custom_font.cget("size"), "bold"),
background='#F7F8FC',
relief='solid',
borderwidth=0,
anchor='center'
)
style.map(
"Custom.Treeview.Heading",
background=[('active', 'white')],
relief=[('active', 'solid')]
)
style.configure("Custom.TButton", font=self.custom_font)
def _configure_scrollbar_style(self) -> None:
"""Configure the style for Scrollbars."""
style = ttk.Style()
style.theme_use("default")
style.configure("Custom.Vertical.TScrollbar",
troughcolor='white',
background='#c6c6c6',
bordercolor='white',
arrowcolor='white',
borderwidth=0,
relief='flat')
style.map("Custom.Vertical.TScrollbar",
background=[('active', '#9b9b9b'), ('pressed', '#8b8b8bff')])
style.layout('Custom.Vertical.TScrollbar',
[('Vertical.Scrollbar.trough',
{'children': [('Vertical.Scrollbar.thumb', {'expand': '1',
'sticky': 'nswe'})],
'sticky': 'ns'})])
def _configure_grid(self) -> None:
"""Configure the grid layout for the main window."""
self.grid_rowconfigure(0, weight=0)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
def _create_widgets(self) -> None:
"""Create and layout all the widgets in the application."""
# Header
self.header_canvas = tk.Canvas(self, bg='white', highlightthickness=0, height=50)
self.header_canvas.grid(row=0, column=1, columnspan=2, sticky='ew')
self.header_canvas.grid_propagate(False)
self.update_header("No Table Selected")
# Sidebar
sidebar = tk.Frame(self, width=200, bg='#F7F8FC')
sidebar.grid(row=1, column=0, rowspan=2, sticky='ns')
sidebar.grid_propagate(False)
self.sidebar = sidebar
# Spacer (for Add and Delete buttons)
spacer = tk.Frame(self, width=100, bg='white')
spacer.grid(row=1, column=1, sticky='ns')
spacer.grid_propagate(False)
self.spacer = spacer
# Content Area
content = tk.Frame(self, bg='white')
content.grid(row=1, column=2, sticky='nsew')
self.content = content
self._create_buttons()
self._create_sidebar()
def update_header(self, text: str) -> None:
"""Update the header with the given text."""
self.header_canvas.delete("all")
small_font = font.Font(family=self.custom_font.actual('family'), size=10)
self.header_canvas.create_text(
10, 10,
anchor='nw',
text="Data >",
fill='#999999',
font=small_font
)
self.header_canvas.create_text(
80, 10,
anchor='nw',
text=text,
fill='black',
font=small_font
)
def _create_sidebar(self) -> None:
"""Create sidebar buttons and list of tables."""
self.create_sidebar_label("Create New Table", self.open_create_table_window, pady=20)
separator = ttk.Separator(self.sidebar, orient='horizontal')
separator.pack(fill='x', pady=10)
self.tables_label = tk.Label(
self.sidebar,
text="Your Tables:",
bg='#F7F8FC',
font=(self.custom_font.cget("family"), self.custom_font.cget("size"), "bold"),
anchor='w',
padx=20
)
self.tables_label.pack(anchor='w')
self.tables_frame = tk.Frame(self.sidebar, bg='#F7F8FC')
self.tables_frame.pack(fill='both', expand=True, padx=10, pady=5)
def create_sidebar_label(self, text: str, command, pady=0) -> None:
"""Create a clickable label in the sidebar."""
label = tk.Label(
self.sidebar,
text=text,
bg='#F7F8FC',
font=self.custom_font,
cursor="hand2",
anchor='w',
padx=20,
pady=5
)
label.pack(pady=(pady, 1), fill='x')
label.bind("<Enter>", lambda e: label.config(bg='#EAEDEF'))
label.bind("<Leave>", lambda e: label.config(bg='#F7F8FC'))
label.bind("<Button-1>", lambda e: command())
def load_tables(self) -> None:
"""Load and display the list of user-defined tables in the sidebar."""
for widget in self.tables_frame.winfo_children():
widget.destroy()
tables = run_query("SELECT name FROM tables")
for (table_name,) in tables:
btn = tk.Button(
self.tables_frame,
text=table_name,
font=self.custom_font,
bg='white',
relief='flat',
anchor='w',
command=lambda tn=table_name: self.show_table(tn)
)
btn.pack(fill='x', pady=2)
btn.bind("<Enter>", lambda e, b=btn: b.config(bg='#EAEDEF'))
btn.bind("<Leave>", lambda e, b=btn: b.config(bg='white'))
def _create_buttons(self) -> None:
"""Create add and delete buttons within the spacer."""
style = ttk.Style()
style.configure("Square.TButton",
font=('Arial', 14),
padding=(1, 1),
width=2,
background='white',
borderwidth=0,
relief='flat')
style.map("Square.TButton",
background=[('active', '#EAEDEF')])
self.add_button = ttk.Button(
self.spacer,
text="+",
command=self.add_entry,
style="Square.TButton"
)
self.delete_button = ttk.Button(
self.spacer,
text="-",
command=self.delete_entry,
style="Square.TButton"
)
# Initially hide buttons until a row is selected
self.add_button.place_forget()
self.delete_button.place_forget()
def open_create_table_window(self) -> None:
"""Open a window to create a new table with specified fields."""
CreateTableWindow(self)
def show_table(self, table_name: str) -> None:
"""Display the selected table's data."""
self.current_table = table_name
self.update_header(table_name)
self.load_table_data()
def load_table_data(self) -> None:
"""Load and display data for the current table."""
self.clear_content()
fields = run_query("SELECT field_name, field_type, is_combo FROM fields WHERE table_name = ?", (self.current_table,))
columns = [field_name for field_name, _, _ in fields]
if 'id' not in columns:
columns.insert(0, 'id') # Ensure 'id' is present
headings = columns.copy()
# Exclude 'id' from display
display_columns = columns.copy()
if 'id' in display_columns:
display_columns.remove('id')
self.setup_treeview(display_columns, display_columns)
for row in run_query(f"SELECT * FROM {self.current_table}"):
display_row = list(row)
if 'id' in columns:
id_index = columns.index('id')
display_row.pop(id_index)
# Process boolean fields
field_types = {field[0]: field[1] for field in fields}
for idx, field in enumerate(display_columns):
if field_types[field].upper() == 'BOOLEAN':
display_row[idx] = self.bool_to_checkbox(display_row[idx])
self.tree.insert('', 'end', iid=str(row[0]), values=display_row)
def clear_content(self) -> None:
"""Clear the content frame by destroying the tree and scrollbar if they exist."""
for widget in [self.tree, self.scrollbar]:
if widget:
widget.destroy()
self.tree = None
self.scrollbar = None
def setup_treeview(self, columns: list, headings: list) -> None:
"""Set up the Treeview widget with specified columns and headings."""
self.tree = ttk.Treeview(
self.content,
columns=columns,
show='headings',
selectmode='browse',
style="Custom.Treeview"
)
for col, head in zip(columns, headings):
self.tree.heading(col, text=head, anchor='center')
anchor = 'w' if head.lower() == 'notes' else 'center'
width = 350
self.tree.column(col, width=width, anchor=anchor)
self.scrollbar = ttk.Scrollbar(
self.content,
orient="vertical",
command=self.tree.yview,
style="Custom.Vertical.TScrollbar"
)
self.tree.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side='right', fill='y')
self.tree.pack(fill='both', expand=True)
self.tree.bind('<<TreeviewSelect>>', self.on_row_select)
self.tree.bind('<Button-1>', self.on_single_click)
self.tree.bind("<Double-Button-1>", self.on_double_click)
self.tree.bind("<Motion>", self.treeview_click_cursor)
def add_entry(self) -> None:
"""Add a new entry to the current table."""
if not self.current_table:
return
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
placeholders = []
values = []
for field_name, field_type in fields:
if field_name == 'id':
continue
if field_type.upper() == 'BOOLEAN':
placeholders.append("?")
values.append(False)
elif field_type.upper() == 'TEXT':
# Check if field has combo options
combo = run_query("""
SELECT COUNT(*) FROM field_options
WHERE table_name = ? AND field_name = ?
""", (self.current_table, field_name))[0][0] > 0
if combo:
placeholders.append("?")
values.append("") # Default empty, user can edit later
else:
placeholders.append("?")
values.append("")
query = f"INSERT INTO {self.current_table} ({', '.join([f for f, _ in fields if f != 'id'])}) VALUES ({', '.join(placeholders)})"
run_query(query, tuple(values))
self.load_table_data()
def delete_entry(self) -> None:
"""Delete the selected entry from the current table."""
selected_item = self.tree.selection()
if not selected_item:
return
row_id = selected_item[0]
run_query(f"DELETE FROM {self.current_table} WHERE id = ?", (row_id,))
self.load_table_data()
def on_row_select(self, event) -> None:
"""Handle the selection of a row in the Treeview."""
selected_item = self.tree.selection()
self.current_row_id = selected_item[0] if selected_item else None
if selected_item:
# Get the bounding box of the selected row
bbox = self.tree.bbox(selected_item)
if bbox:
x, y, width, height = bbox
y_offset = y + height // 2
# Position the delete button
self.delete_button.place(x=45, y=y + height//2, anchor='center')
# Position the add button
self.add_button.place(x=80, y=y + height//2, anchor='center')
else:
self.delete_button.place_forget()
self.add_button.place_forget()
def on_single_click(self, event) -> None:
"""Handle single click events for toggling boolean fields."""
region = self.tree.identify("region", event.x, event.y)
if region != "cell":
return
row_id = self.tree.identify_row(event.y)
column = self.tree.identify_column(event.x)
if not row_id or not column:
return
try:
col_index = int(column.replace('#', '')) - 1
column_name = self.tree['columns'][col_index]
except (IndexError, ValueError):
return
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
if column_name in field_types and field_types[column_name].upper() == 'BOOLEAN':
current_value = self.tree.set(row_id, column_name)
new_value = not self.checkbox_to_bool(current_value)
self.tree.set(row_id, column_name, self.bool_to_checkbox(new_value))
self.update_database(row_id, column_name, new_value)
def on_double_click(self, event) -> None:
"""Open an entry or combo box to edit the item on double-click."""
region = self.tree.identify("region", event.x, event.y)
if region != "cell":
return
column = self.tree.identify_column(event.x)
row_id = self.tree.identify_row(event.y)
if not row_id or not column:
return
try:
col_index = int(column.replace('#', '')) - 1
column_name = self.tree['columns'][col_index]
except (IndexError, ValueError):
return
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
x, y, width, height = self.tree.bbox(row_id, column)
current_value = self.tree.set(row_id, column_name)
if field_types.get(column_name, '').upper() == 'TEXT':
# Check if field has combo options
options = run_query("""
SELECT option_value FROM field_options
WHERE table_name = ? AND field_name = ?
""", (self.current_table, column_name))
options = [opt[0] for opt in options]
if options:
combobox = ttk.Combobox(self.content, values=options, state='readonly', font=self.custom_font)
combobox.place(x=x, y=y, width=width, height=height)
combobox.set(current_value)
combobox.focus()
def on_select(event):
new_value = combobox.get()
self.tree.set(row_id, column_name, new_value)
combobox.destroy()
self.update_database(row_id, column_name, new_value)
combobox.bind("<<ComboboxSelected>>", on_select)
combobox.bind("<FocusOut>", lambda e: combobox.destroy())
return
entry = tk.Entry(self.content, font=self.custom_font, relief='flat', borderwidth=0)
entry.place(x=x, y=y, width=width, height=height)
entry.insert(0, current_value)
entry.focus()
def on_focus_out(event):
new_value = entry.get()
self.tree.set(row_id, column_name, new_value)
entry.destroy()
self.update_database(row_id, column_name, new_value)
entry.bind("<FocusOut>", on_focus_out)
entry.bind("<Return>", lambda event: on_focus_out(event))
def update_database(self, row_id: str, column: str, new_value) -> None:
"""Update the database with the modified cell value."""
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
query = f"UPDATE {self.current_table} SET {column} = ? WHERE id = ?"
if field_types[column].upper() == 'BOOLEAN':
parameters = (new_value, row_id)
else:
parameters = (new_value, row_id)
run_query(query, parameters)
def treeview_click_cursor(self, event) -> None:
"""Change the cursor to a hand when hovering over boolean columns."""
region = self.tree.identify("region", event.x, event.y)
if region == "cell":
column = self.tree.identify_column(event.x)
try:
col_index = int(column.replace('#', '')) - 1
column_name = self.tree['columns'][col_index]
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
cursor = "hand2" if field_types.get(column_name, '').upper() == 'BOOLEAN' else ""
self.tree.configure(cursor=cursor)
except (IndexError, ValueError):
self.tree.configure(cursor="")
else:
self.tree.configure(cursor="")
@staticmethod
def bool_to_checkbox(value: bool) -> str:
"""Convert a boolean value to a checkbox symbol."""
return '☑' if value else '☐'
@staticmethod
def checkbox_to_bool(value: str) -> bool:
"""Convert a checkbox symbol to a boolean value."""
return value == '☑'
class CreateTableWindow(tk.Toplevel):
"""A window to create a new table with specified fields."""
code
def __init__(self, parent: MediaTracker):
super().__init__(parent)
self.parent = parent
self.title("Create New Table")
self.geometry("500x400")
self.configure(bg='white')
self.custom_font = parent.custom_font
self.fields = []
self.combo_fields = {} # field_name: [options]
self._create_widgets()
def _create_widgets(self) -> None:
"""Create widgets for table creation."""
title = tk.Label(self, text="Create New Table", font=(self.custom_font.cget("family"), 14, "bold"), bg='white')
title.pack(pady=10)
name_frame = tk.Frame(self, bg='white')
name_frame.pack(pady=5, fill='x', padx=20)
tk.Label(name_frame, text="Table Name:", font=self.custom_font, bg='white').pack(side='left')
self.table_name_entry = tk.Entry(name_frame, font=self.custom_font)
self.table_name_entry.pack(side='left', fill='x', expand=True, padx=10)
fields_label = tk.Label(self, text="Fields:", font=(self.custom_font.cget("family"), 12, "bold"), bg='white')
fields_label.pack(pady=10)
self.fields_frame = tk.Frame(self, bg='white')
self.fields_frame.pack(fill='both', expand=True, padx=20, pady=5)
# Header for fields
header = tk.Frame(self.fields_frame, bg='white')
header.pack(fill='x')
tk.Label(header, text="Field Name", font=self.custom_font, bg='white').grid(row=0, column=0, padx=5, pady=5)
tk.Label(header, text="Field Type", font=self.custom_font, bg='white').grid(row=0, column=1, padx=5, pady=5)
tk.Label(header, text="Combo Options", font=self.custom_font, bg='white').grid(row=0, column=2, padx=5, pady=5)
tk.Label(header, text="", bg='white').grid(row=0, column=3, padx=5, pady=5)
self.fields_container = tk.Frame(self.fields_frame, bg='white')
self.fields_container.pack(fill='both', expand=True)
add_field_btn = ttk.Button(self, text="Add Field", command=self.add_field_row)
add_field_btn.pack(pady=10)
create_btn = ttk.Button(self, text="Create Table", command=self.create_table)
create_btn.pack(pady=5)
def add_field_row(self) -> None:
"""Add a new row to define a field."""
row = len(self.fields)
field_name_var = tk.StringVar()
field_type_var = tk.StringVar(value="TEXT")
combo_var = tk.StringVar()
frame = tk.Frame(self.fields_container, bg='white')
frame.pack(fill='x', pady=2)
entry = tk.Entry(frame, textvariable=field_name_var, font=self.custom_font)
entry.grid(row=0, column=0, padx=5, pady=2)
combo_type = ttk.Combobox(frame, textvariable=field_type_var, values=["TEXT", "BOOLEAN"], state='readonly', width=10, font=self.custom_font)
combo_type.grid(row=0, column=1, padx=5, pady=2)
combo_entry = tk.Entry(frame, textvariable=combo_var, font=self.custom_font, state='disabled')
combo_entry.grid(row=0, column=2, padx=5, pady=2)
def toggle_combo(*args):
if field_type_var.get() == "TEXT":
combo_entry.config(state='normal')
else:
combo_entry.delete(0, tk.END)
combo_entry.config(state='disabled')
field_type_var.trace('w', toggle_combo)
manage_btn = tk.Button(frame, text="Manage", command=lambda: self.manage_combo_options(combo_var))
manage_btn.grid(row=0, column=3, padx=5, pady=2)
manage_btn.config(state='disabled')
def enable_manage(*args):
if field_type_var.get() == "TEXT":
manage_btn.config(state='normal')
else:
manage_btn.config(state='disabled')
field_type_var.trace('w', enable_manage)
self.fields.append({
'name_var': field_name_var,
'type_var': field_type_var,
'combo_var': combo_var,
'frame': frame
})
def manage_combo_options(self, combo_var: tk.StringVar) -> None:
"""Open a window to manage combo box options for a TEXT field."""
ManageComboOptionsWindow(self, combo_var)
def create_table(self) -> None:
"""Create the table in the database based on user input."""
table_name = self.table_name_entry.get().strip()
if not table_name:
return # Optionally, highlight the entry field
# Check if table already exists
existing_tables = run_query("SELECT name FROM tables WHERE name = ?", (table_name,))
if existing_tables:
return # Optionally, notify the user
fields_definitions = []
fields = []
for field in self.fields:
field_name = field['name_var'].get().strip()
field_type = field['type_var'].get().strip().upper()
is_combo = 0
if not field_name or field_type not in ["TEXT", "BOOLEAN"]:
return # Optionally, highlight invalid fields
if field_type == "TEXT" and ',' in field['combo_var'].get():
is_combo = 1
elif field_type == "TEXT" and field['combo_var'].get().strip() != "":
is_combo = 1
fields_definitions.append((field_name, field_type, is_combo))
fields.append(field_name)
# Create the table with an auto-incremented hidden 'id' field
create_query = f"CREATE TABLE {table_name} (id INTEGER PRIMARY KEY AUTOINCREMENT"
for field_name, field_type, is_combo in fields_definitions:
if field_type == "TEXT":
create_query += f", {field_name} TEXT"
elif field_type == "BOOLEAN":
create_query += f", {field_name} BOOLEAN"
create_query += ");"
run_query(create_query)
# Insert table metadata
run_query("INSERT INTO tables (name) VALUES (?)", (table_name,))
for field_name, field_type, is_combo in fields_definitions:
run_query("""
INSERT INTO fields (table_name, field_name, field_type, is_combo)
VALUES (?, ?, ?, ?)
""", (table_name, field_name, field_type, is_combo))
if is_combo:
# Insert default empty options
pass # Users can manage options later
self.parent.load_tables()
self.destroy()
class ManageComboOptionsWindow(tk.Toplevel):
"""A window to manage combo box options for a TEXT field."""
code
def __init__(self, parent: CreateTableWindow, combo_var: tk.StringVar):
super().__init__(parent)
self.parent = parent
self.combo_var = combo_var
self.title("Manage Combo Options")
self.geometry("400x300")
self.configure(bg='white')
self.custom_font = parent.custom_font
self.options = []
self._create_widgets()
def _create_widgets(self) -> None:
"""Create widgets for managing combo options."""
title = tk.Label(self, text="Manage Combo Options", font=(self.custom_font.cget("family"), 14, "bold"), bg='white')
title.pack(pady=10)
self.listbox = tk.Listbox(self, font=self.custom_font)
self.listbox.pack(fill='both', expand=True, padx=20, pady=10)
button_frame = tk.Frame(self, bg='white')
button_frame.pack(pady=5)
add_btn = ttk.Button(button_frame, text="Add", command=self.add_option)
add_btn.pack(side='left', padx=5)
remove_btn = ttk.Button(button_frame, text="Remove", command=self.remove_option)
remove_btn.pack(side='left', padx=5)
save_btn = ttk.Button(self, text="Save", command=self.save_options)
save_btn.pack(pady=10)
def add_option(self) -> None:
"""Add a new option to the list."""
option = simpledialog.askstring("Add Option", "Enter new option:", parent=self)
if option:
self.listbox.insert(tk.END, option)
def remove_option(self) -> None:
"""Remove the selected option from the list."""
selected_indices = self.listbox.curselection()
for index in reversed(selected_indices):
self.listbox.delete(index)
def save_options(self) -> None:
"""Save the options to the combo_var and close the window."""
options = self.listbox.get(0, tk.END)
self.combo_var.set(",".join(options))
self.destroy()
if name == "main":
initialize_database()
app = MediaTracker()
app.mainloop()
Certainly! To modify your Tkinter application so that the sidebar covers the entire height of the program and the header is shifted to the right to make space for the sidebar, you'll need to adjust the grid configuration and widget placement within your MediaTracker class.
Below is the complete modified code with detailed comments explaining the changes:
python
import sys
import sqlite3
from pathlib import Path
import tkinter as tk
from tkinter import ttk, messagebox, font, simpledialog
MEDIA_DB = Path(__file__).parent / 'media.db'
if sys.platform.startswith('win'):
try:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception as e:
print("Error setting DPI awareness:", e)
def run_query(query: str, parameters: tuple = ()) -> list:
"""Execute a SQL query and return the results if it's a SELECT statement."""
with sqlite3.connect(MEDIA_DB) as conn:
cursor = conn.cursor()
result = cursor.execute(query, parameters)
if query.strip().lower().startswith("select"):
return result.fetchall()
conn.commit()
def initialize_database() -> None:
"""Initialize the database with necessary metadata tables."""
run_query('''
CREATE TABLE IF NOT EXISTS tables (
name TEXT PRIMARY KEY
);
''')
run_query('''
CREATE TABLE IF NOT EXISTS fields (
id INTEGER PRIMARY KEY AUTOINCREMENT,
table_name TEXT,
field_name TEXT,
field_type TEXT,
is_combo BOOLEAN,
FOREIGN KEY(table_name) REFERENCES tables(name)
);
''')
run_query('''
CREATE TABLE IF NOT EXISTS field_options (
id INTEGER PRIMARY KEY AUTOINCREMENT,
table_name TEXT,
field_name TEXT,
option_value TEXT,
FOREIGN KEY(table_name) REFERENCES tables(name)
);
''')
class MediaTracker(tk.Tk):
"""A Tkinter application to track user-defined media databases."""
def __init__(self):
super().__init__()
self.configure(bg='white')
self.title("Media Tracker")
self.geometry("1200x600")
self.custom_font = self._get_custom_font()
self.current_table = None
self.current_row_id = None
self.tree = None
self.scrollbar = None
self._configure_style()
self._configure_scrollbar_style()
self._configure_grid()
self._create_widgets()
self.load_tables()
def _get_custom_font(self) -> font.Font:
"""Retrieve the custom font, defaulting to Arial if Literata is unavailable."""
try:
return font.Font(family='Literata', size=12)
except:
return font.Font(family='Arial', size=12)
def _configure_style(self) -> None:
"""Configure the styles for Treeview and buttons."""
font_line_space = self.custom_font.metrics('linespace')
row_height = font_line_space + 4
style = ttk.Style()
style.theme_use("default")
style.layout(
'Custom.Treeview',
[('Custom.Treeview.treearea', {'sticky': 'nsew'})]
)
style.configure(
"Custom.Treeview",
font=self.custom_font,
rowheight=row_height,
borderwidth=0,
relief='flat',
highlightthickness=0,
background='white',
fieldbackground='white'
)
style.configure(
"Custom.Treeview.Heading",
font=(self.custom_font.cget("family"), self.custom_font.cget("size"), "bold"),
background='#F7F8FC',
relief='solid',
borderwidth=0,
anchor='center'
)
style.map(
"Custom.Treeview.Heading",
background=[('active', 'white')],
relief=[('active', 'solid')]
)
style.configure("Custom.TButton", font=self.custom_font)
def _configure_scrollbar_style(self) -> None:
"""Configure the style for Scrollbars."""
style = ttk.Style()
style.theme_use("default")
style.configure("Custom.Vertical.TScrollbar",
troughcolor='white',
background='#c6c6c6',
bordercolor='white',
arrowcolor='white',
borderwidth=0,
relief='flat')
style.map("Custom.Vertical.TScrollbar",
background=[('active', '#9b9b9b'), ('pressed', '#8b8b8bff')])
style.layout('Custom.Vertical.TScrollbar',
[('Vertical.Scrollbar.trough',
{'children': [('Vertical.Scrollbar.thumb', {'expand': '1',
'sticky': 'nswe'})],
'sticky': 'ns'})])
def _configure_grid(self) -> None:
"""Configure the grid layout for the main window."""
# Configure rows: 0 for header, 1 for main content
self.grid_rowconfigure(0, weight=0)
self.grid_rowconfigure(1, weight=1)
# Configure columns: 0 for sidebar, 1 for header and content
self.grid_columnconfigure(0, weight=0, minsize=200) # Sidebar fixed width
self.grid_columnconfigure(1, weight=1) # Main area
def _create_widgets(self) -> None:
"""Create and layout all the widgets in the application."""
# Sidebar - Placed in column 0, spans both rows
sidebar = tk.Frame(self, width=200, bg='#F7F8FC')
sidebar.grid(row=0, column=0, rowspan=2, sticky='ns')
sidebar.grid_propagate(False)
self.sidebar = sidebar
self._create_sidebar()
# Main Frame - Contains header and content
main_frame = tk.Frame(self, bg='white')
main_frame.grid(row=0, column=1, rowspan=2, sticky='nsew')
main_frame.grid_rowconfigure(0, weight=0) # Header row
main_frame.grid_rowconfigure(1, weight=1) # Content row
main_frame.grid_columnconfigure(0, weight=1)
# Header
self.header_canvas = tk.Canvas(main_frame, bg='white', highlightthickness=0, height=50)
self.header_canvas.grid(row=0, column=0, sticky='ew')
self.header_canvas.grid_propagate(False)
self.update_header("No Table Selected")
# Spacer and Buttons are now part of the content frame
# Content Area
content = tk.Frame(main_frame, bg='white')
content.grid(row=1, column=0, sticky='nsew')
content.grid_rowconfigure(0, weight=1)
content.grid_columnconfigure(0, weight=1)
self.content = content
# Spacer Frame inside Content to hold Add and Delete buttons
self.spacer = tk.Frame(content, width=100, bg='white')
self.spacer.grid(row=0, column=0, sticky='nw')
self.spacer.grid_propagate(False)
# Treeview fills the remaining space
self.tree_wrapper = tk.Frame(content, bg='white')
self.tree_wrapper.grid(row=0, column=1, sticky='nsew')
self.tree_wrapper.grid_rowconfigure(0, weight=1)
self.tree_wrapper.grid_columnconfigure(0, weight=1)
self._create_buttons()
def update_header(self, text: str) -> None:
"""Update the header with the given text."""
self.header_canvas.delete("all")
small_font = font.Font(family=self.custom_font.actual('family'), size=10)
self.header_canvas.create_text(
10, 10,
anchor='nw',
text="Data >",
fill='#999999',
font=small_font
)
self.header_canvas.create_text(
80, 10,
anchor='nw',
text=text,
fill='black',
font=small_font
)
def _create_sidebar(self) -> None:
"""Create sidebar buttons and list of tables."""
self.create_sidebar_label("Create New Table", self.open_create_table_window, pady=20)
separator = ttk.Separator(self.sidebar, orient='horizontal')
separator.pack(fill='x', pady=10)
self.tables_label = tk.Label(
self.sidebar,
text="Your Tables:",
bg='#F7F8FC',
font=(self.custom_font.cget("family"), self.custom_font.cget("size"), "bold"),
anchor='w',
padx=20
)
self.tables_label.pack(anchor='w')
self.tables_frame = tk.Frame(self.sidebar, bg='#F7F8FC')
self.tables_frame.pack(fill='both', expand=True, padx=10, pady=5)
def create_sidebar_label(self, text: str, command, pady=0) -> None:
"""Create a clickable label in the sidebar."""
label = tk.Label(
self.sidebar,
text=text,
bg='#F7F8FC',
font=self.custom_font,
cursor="hand2",
anchor='w',
padx=20,
pady=5
)
label.pack(pady=(pady, 1), fill='x')
label.bind("<Enter>", lambda e: label.config(bg='#EAEDEF'))
label.bind("<Leave>", lambda e: label.config(bg='#F7F8FC'))
label.bind("<Button-1>", lambda e: command())
def load_tables(self) -> None:
"""Load and display the list of user-defined tables in the sidebar."""
for widget in self.tables_frame.winfo_children():
widget.destroy()
tables = run_query("SELECT name FROM tables")
for (table_name,) in tables:
btn = tk.Button(
self.tables_frame,
text=table_name,
font=self.custom_font,
bg='white',
relief='flat',
anchor='w',
command=lambda tn=table_name: self.show_table(tn)
)
btn.pack(fill='x', pady=2)
btn.bind("<Enter>", lambda e, b=btn: b.config(bg='#EAEDEF'))
btn.bind("<Leave>", lambda e, b=btn: b.config(bg='white'))
def _create_buttons(self) -> None:
"""Create add and delete buttons within the spacer."""
style = ttk.Style()
style.configure("Square.TButton",
font=('Arial', 14),
padding=(1, 1),
width=2,
background='white',
borderwidth=0,
relief='flat')
style.map("Square.TButton",
background=[('active', '#EAEDEF')])
self.add_button = ttk.Button(
self.spacer,
text="+",
command=self.add_entry,
style="Square.TButton"
)
self.delete_button = ttk.Button(
self.spacer,
text="-",
command=self.delete_entry,
style="Square.TButton"
)
# Initially hide buttons until a row is selected
self.add_button.place_forget()
self.delete_button.place_forget()
def open_create_table_window(self) -> None:
"""Open a window to create a new table with specified fields."""
CreateTableWindow(self)
def show_table(self, table_name: str) -> None:
"""Display the selected table's data."""
self.current_table = table_name
self.update_header(table_name)
self.load_table_data()
def load_table_data(self) -> None:
"""Load and display data for the current table."""
self.clear_content()
fields = run_query("SELECT field_name, field_type, is_combo FROM fields WHERE table_name = ?", (self.current_table,))
columns = [field_name for field_name, _, _ in fields]
if 'id' not in columns:
columns.insert(0, 'id') # Ensure 'id' is present
headings = columns.copy()
# Exclude 'id' from display
display_columns = columns.copy()
if 'id' in display_columns:
display_columns.remove('id')
self.setup_treeview(display_columns, display_columns)
for row in run_query(f"SELECT * FROM {self.current_table}"):
display_row = list(row)
if 'id' in columns:
id_index = columns.index('id')
display_row.pop(id_index)
# Process boolean fields
field_types = {field[0]: field[1] for field in fields}
for idx, field in enumerate(display_columns):
if field_types[field].upper() == 'BOOLEAN':
display_row[idx] = self.bool_to_checkbox(display_row[idx])
self.tree.insert('', 'end', iid=str(row[0]), values=display_row)
def clear_content(self) -> None:
"""Clear the content frame by destroying the tree and scrollbar if they exist."""
for widget in [self.tree, self.scrollbar]:
if widget:
widget.destroy()
self.tree = None
self.scrollbar = None
def setup_treeview(self, columns: list, headings: list) -> None:
"""Set up the Treeview widget with specified columns and headings."""
self.tree = ttk.Treeview(
self.tree_wrapper,
columns=columns,
show='headings',
selectmode='browse',
style="Custom.Treeview"
)
for col, head in zip(columns, headings):
self.tree.heading(col, text=head, anchor='center')
anchor = 'w' if head.lower() == 'notes' else 'center'
width = 350
self.tree.column(col, width=width, anchor=anchor)
self.scrollbar = ttk.Scrollbar(
self.tree_wrapper,
orient="vertical",
command=self.tree.yview,
style="Custom.Vertical.TScrollbar"
)
self.tree.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side='right', fill='y')
self.tree.pack(fill='both', expand=True)
self.tree.bind('<<TreeviewSelect>>', self.on_row_select)
self.tree.bind('<Button-1>', self.on_single_click)
self.tree.bind("<Double-Button-1>", self.on_double_click)
self.tree.bind("<Motion>", self.treeview_click_cursor)
def add_entry(self) -> None:
"""Add a new entry to the current table."""
if not self.current_table:
return
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
placeholders = []
values = []
for field_name, field_type in fields:
if field_name == 'id':
continue
if field_type.upper() == 'BOOLEAN':
placeholders.append("?")
values.append(False)
elif field_type.upper() == 'TEXT':
# Check if field has combo options
combo = run_query("""
SELECT COUNT(*) FROM field_options
WHERE table_name = ? AND field_name = ?
""", (self.current_table, field_name))[0][0] > 0
if combo:
placeholders.append("?")
values.append("") # Default empty, user can edit later
else:
placeholders.append("?")
values.append("")
query = f"INSERT INTO {self.current_table} ({', '.join([f for f, _ in fields if f != 'id'])}) VALUES ({', '.join(placeholders)})"
run_query(query, tuple(values))
self.load_table_data()
def delete_entry(self) -> None:
"""Delete the selected entry from the current table."""
selected_item = self.tree.selection()
if not selected_item:
return
row_id = selected_item[0]
run_query(f"DELETE FROM {self.current_table} WHERE id = ?", (row_id,))
self.load_table_data()
def on_row_select(self, event) -> None:
"""Handle the selection of a row in the Treeview."""
selected_item = self.tree.selection()
self.current_row_id = selected_item[0] if selected_item else None
if selected_item:
# Get the bounding box of the selected row
bbox = self.tree.bbox(selected_item)
if bbox:
x, y, width, height = bbox
y_offset = y + height // 2
# Position the delete button within the spacer
self.delete_button.place(x=45, y=y + height//2, anchor='center')
# Position the add button within the spacer
self.add_button.place(x=80, y=y + height//2, anchor='center')
else:
self.delete_button.place_forget()
self.add_button.place_forget()
def on_single_click(self, event) -> None:
"""Handle single click events for toggling boolean fields."""
region = self.tree.identify("region", event.x, event.y)
if region != "cell":
return
row_id = self.tree.identify_row(event.y)
column = self.tree.identify_column(event.x)
if not row_id or not column:
return
try:
col_index = int(column.replace('#', '')) - 1
column_name = self.tree['columns'][col_index]
except (IndexError, ValueError):
return
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
if column_name in field_types and field_types[column_name].upper() == 'BOOLEAN':
current_value = self.tree.set(row_id, column_name)
new_value = not self.checkbox_to_bool(current_value)
self.tree.set(row_id, column_name, self.bool_to_checkbox(new_value))
self.update_database(row_id, column_name, new_value)
def on_double_click(self, event) -> None:
"""Open an entry or combo box to edit the item on double-click."""
region = self.tree.identify("region", event.x, event.y)
if region != "cell":
return
column = self.tree.identify_column(event.x)
row_id = self.tree.identify_row(event.y)
if not row_id or not column:
return
try:
col_index = int(column.replace('#', '')) - 1
column_name = self.tree['columns'][col_index]
except (IndexError, ValueError):
return
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
x, y, width, height = self.tree.bbox(row_id, column)
current_value = self.tree.set(row_id, column_name)
if field_types.get(column_name, '').upper() == 'TEXT':
# Check if field has combo options
options = run_query("""
SELECT option_value FROM field_options
WHERE table_name = ? AND field_name = ?
""", (self.current_table, column_name))
options = [opt[0] for opt in options]
if options:
combobox = ttk.Combobox(self.content, values=options, state='readonly', font=self.custom_font)
combobox.place(x=x, y=y, width=width, height=height)
combobox.set(current_value)
combobox.focus()
def on_select(event):
new_value = combobox.get()
self.tree.set(row_id, column_name, new_value)
combobox.destroy()
self.update_database(row_id, column_name, new_value)
combobox.bind("<<ComboboxSelected>>", on_select)
combobox.bind("<FocusOut>", lambda e: combobox.destroy())
return
entry = tk.Entry(self.content, font=self.custom_font, relief='flat', borderwidth=0)
entry.place(x=x, y=y, width=width, height=height)
entry.insert(0, current_value)
entry.focus()
def on_focus_out(event):
new_value = entry.get()
self.tree.set(row_id, column_name, new_value)
entry.destroy()
self.update_database(row_id, column_name, new_value)
entry.bind("<FocusOut>", on_focus_out)
entry.bind("<Return>", lambda event: on_focus_out(event))
def update_database(self, row_id: str, column: str, new_value) -> None:
"""Update the database with the modified cell value."""
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
query = f"UPDATE {self.current_table} SET {column} = ? WHERE id = ?"
if field_types[column].upper() == 'BOOLEAN':
parameters = (new_value, row_id)
else:
parameters = (new_value, row_id)
run_query(query, parameters)
def treeview_click_cursor(self, event) -> None:
"""Change the cursor to a hand when hovering over boolean columns."""
region = self.tree.identify("region", event.x, event.y)
if region == "cell":
column = self.tree.identify_column(event.x)
try:
col_index = int(column.replace('#', '')) - 1
column_name = self.tree['columns'][col_index]
fields = run_query("SELECT field_name, field_type FROM fields WHERE table_name = ?", (self.current_table,))
field_types = {field[0]: field[1] for field in fields}
cursor = "hand2" if field_types.get(column_name, '').upper() == 'BOOLEAN' else ""
self.tree.configure(cursor=cursor)
except (IndexError, ValueError):
self.tree.configure(cursor="")
else:
self.tree.configure(cursor="")
@staticmethod
def bool_to_checkbox(value: bool) -> str:
"""Convert a boolean value to a checkbox symbol."""
return '☑' if value else '☐'
@staticmethod
def checkbox_to_bool(value: str) -> bool:
"""Convert a checkbox symbol to a boolean value."""
return value == '☑'
class CreateTableWindow(tk.Toplevel):
"""A window to create a new table with specified fields."""
def __init__(self, parent: MediaTracker):
super().__init__(parent)
self.parent = parent
self.title("Create New Table")
self.geometry("500x400")
self.configure(bg='white')
self.custom_font = parent.custom_font
self.fields = []
self.combo_fields = {} # field_name: [options]
self._create_widgets()
def _create_widgets(self) -> None:
"""Create widgets for table creation."""
title = tk.Label(self, text="Create New Table", font=(self.custom_font.cget("family"), 14, "bold"), bg='white')
title.pack(pady=10)
name_frame = tk.Frame(self, bg='white')
name_frame.pack(pady=5, fill='x', padx=20)
tk.Label(name_frame, text="Table Name:", font=self.custom_font, bg='white').pack(side='left')
self.table_name_entry = tk.Entry(name_frame, font=self.custom_font)
self.table_name_entry.pack(side='left', fill='x', expand=True, padx=10)
fields_label = tk.Label(self, text="Fields:", font=(self.custom_font.cget("family"), 12, "bold"), bg='white')
fields_label.pack(pady=10)
self.fields_frame = tk.Frame(self, bg='white')
self.fields_frame.pack(fill='both', expand=True, padx=20, pady=5)
# Header for fields
header = tk.Frame(self.fields_frame, bg='white')
header.pack(fill='x')
tk.Label(header, text="Field Name", font=self.custom_font, bg='white').grid(row=0, column=0, padx=5, pady=5)
tk.Label(header, text="Field Type", font=self.custom_font, bg='white').grid(row=0, column=1, padx=5, pady=5)
tk.Label(header, text="Combo Options", font=self.custom_font, bg='white').grid(row=0, column=2, padx=5, pady=5)
tk.Label(header, text="", bg='white').grid(row=0, column=3, padx=5, pady=5)
self.fields_container = tk.Frame(self.fields_frame, bg='white')
self.fields_container.pack(fill='both', expand=True)
add_field_btn = ttk.Button(self, text="Add Field", command=self.add_field_row)
add_field_btn.pack(pady=10)
create_btn = ttk.Button(self, text="Create Table", command=self.create_table)
create_btn.pack(pady=5)
def add_field_row(self) -> None:
"""Add a new row to define a field."""
row = len(self.fields)
field_name_var = tk.StringVar()
field_type_var = tk.StringVar(value="TEXT")
combo_var = tk.StringVar()
frame = tk.Frame(self.fields_container, bg='white')
frame.pack(fill='x', pady=2)
entry = tk.Entry(frame, textvariable=field_name_var, font=self.custom_font)
entry.grid(row=0, column=0, padx=5, pady=2)
combo_type = ttk.Combobox(frame, textvariable=field_type_var, values=["TEXT", "BOOLEAN"], state='readonly', width=10, font=self.custom_font)
combo_type.grid(row=0, column=1, padx=5, pady=2)
combo_entry = tk.Entry(frame, textvariable=combo_var, font=self.custom_font, state='disabled')
combo_entry.grid(row=0, column=2, padx=5, pady=2)
def toggle_combo(*args):
if field_type_var.get() == "TEXT":
combo_entry.config(state='normal')
else:
combo_entry.delete(0, tk.END)
combo_entry.config(state='disabled')
field_type_var.trace('w', toggle_combo)
manage_btn = tk.Button(frame, text="Manage", command=lambda: self.manage_combo_options(combo_var))
manage_btn.grid(row=0, column=3, padx=5, pady=2)
manage_btn.config(state='disabled')
def enable_manage(*args):
if field_type_var.get() == "TEXT":
manage_btn.config(state='normal')
else:
manage_btn.config(state='disabled')
field_type_var.trace('w', enable_manage)
self.fields.append({
'name_var': field_name_var,
'type_var': field_type_var,
'combo_var': combo_var,
'frame': frame
})
def manage_combo_options(self, combo_var: tk.StringVar) -> None:
"""Open a window to manage combo box options for a TEXT field."""
ManageComboOptionsWindow(self, combo_var)
def create_table(self) -> None:
"""Create the table in the database based on user input."""
table_name = self.table_name_entry.get().strip()
if not table_name:
messagebox.showerror("Error", "Table name cannot be empty.")
return
# Check if table already exists
existing_tables = run_query("SELECT name FROM tables WHERE name = ?", (table_name,))
if existing_tables:
messagebox.showerror("Error", f"Table '{table_name}' already exists.")
return
fields_definitions = []
fields = []
for field in self.fields:
field_name = field['name_var'].get().strip()
field_type = field['type_var'].get().strip().upper()
is_combo = 0
if not field_name or field_type not in ["TEXT", "BOOLEAN"]:
messagebox.showerror("Error", "All fields must have a valid name and type.")
return
if field_type == "TEXT" and (',' in field['combo_var'].get() or field['combo_var'].get().strip() != ""):
is_combo = 1
fields_definitions.append((field_name, field_type, is_combo))
fields.append(field_name)
# Create the table with an auto-incremented hidden 'id' field
create_query = f"CREATE TABLE {table_name} (id INTEGER PRIMARY KEY AUTOINCREMENT"
for field_name, field_type, is_combo in fields_definitions:
if field_type == "TEXT":
create_query += f", {field_name} TEXT"
elif field_type == "BOOLEAN":
create_query += f", {field_name} BOOLEAN"
create_query += ");"
run_query(create_query)
# Insert table metadata
run_query("INSERT INTO tables (name) VALUES (?)", (table_name,))
for field_name, field_type, is_combo in fields_definitions:
run_query("""
INSERT INTO fields (table_name, field_name, field_type, is_combo)
VALUES (?, ?, ?, ?)
""", (table_name, field_name, field_type, is_combo))
if is_combo:
# Insert default empty options
pass # Users can manage options later
self.parent.load_tables()
self.destroy()
class ManageComboOptionsWindow(tk.Toplevel):
"""A window to manage combo box options for a TEXT field."""
def __init__(self, parent: CreateTableWindow, combo_var: tk.StringVar):
super().__init__(parent)
self.parent = parent
self.combo_var = combo_var
self.title("Manage Combo Options")
self.geometry("400x300")
self.configure(bg='white')
self.custom_font = parent.custom_font
self.options = []
self._create_widgets()
def _create_widgets(self) -> None:
"""Create widgets for managing combo options."""
title = tk.Label(self, text="Manage Combo Options", font=(self.custom_font.cget("family"), 14, "bold"), bg='white')
title.pack(pady=10)
self.listbox = tk.Listbox(self, font=self.custom_font)
self.listbox.pack(fill='both', expand=True, padx=20, pady=10)
button_frame = tk.Frame(self, bg='white')
button_frame.pack(pady=5)
add_btn = ttk.Button(button_frame, text="Add", command=self.add_option)
add_btn.pack(side='left', padx=5)
remove_btn = ttk.Button(button_frame, text="Remove", command=self.remove_option)
remove_btn.pack(side='left', padx=5)
save_btn = ttk.Button(self, text="Save", command=self.save_options)
save_btn.pack(pady=10)
def add_option(self) -> None:
"""Add a new option to the list."""
option = simpledialog.askstring("Add Option", "Enter new option:", parent=self)
if option:
self.listbox.insert(tk.END, option)
def remove_option(self) -> None:
"""Remove the selected option from the list."""
selected_indices = self.listbox.curselection()
for index in reversed(selected_indices):
self.listbox.delete(index)
def save_options(self) -> None:
"""Save the options to the combo_var and close the window."""
options = self.listbox.get(0, tk.END)
self.combo_var.set(",".join(options))
self.destroy()
if __name__ == "__main__":
initialize_database()
app = MediaTracker()
app.mainloop()
Key Modifications Explained:
Grid Configuration (_configure_grid):
Columns:
Column 0: Dedicated to the sidebar with a fixed width (minsize=200) and does not expand (weight=0).
Column 1: Main area that expands to fill available space (weight=1).
Rows:
Row 0: For the header with no vertical expansion (weight=0).
Row 1: For the main content area that expands vertically (weight=1).
python
def _configure_grid(self) -> None:
"""Configure the grid layout for the main window."""
# Configure rows: 0 for header, 1 for main content
self.grid_rowconfigure(0, weight=0)
self.grid_rowconfigure(1, weight=1)
# Configure columns: 0 for sidebar, 1 for header and content
self.grid_columnconfigure(0, weight=0, minsize=200) # Sidebar fixed width
self.grid_columnconfigure(1, weight=1) # Main area
Widget Placement (_create_widgets):
Sidebar:
Placed in row 0, column 0, spanning both rows using rowspan=2.
Set to stick to the north and south edges (sticky='ns') to cover the entire height.
Main Frame:
Placed in row 0, column 1, spanning both rows.
Contains the header and content areas arranged vertically.
Header:
Placed at the top of the main frame in row 0, column 0 within the main frame.
Content Area:
Placed below the header in row 1, column 0 within the main frame.
Contains the spacer for Add/Delete buttons and the Treeview.
def _create_widgets(self) -> None:
"""Create and layout all the widgets in the application."""
# Sidebar - Placed in column 0, spans both rows
sidebar = tk.Frame(self, width=200, bg='#F7F8FC')
sidebar.grid(row=0, column=0, rowspan=2, sticky='ns')
sidebar.grid_propagate(False)
self.sidebar = sidebar
self._create_sidebar()
# Main Frame - Contains header and content
main_frame = tk.Frame(self, bg='white')
main_frame.grid(row=0, column=1, rowspan=2, sticky='nsew')
main_frame.grid_rowconfigure(0, weight=0) # Header row
main_frame.grid_rowconfigure(1, weight=1) # Content row
main_frame.grid_columnconfigure(0, weight=1)
# Header
self.header_canvas = tk.Canvas(main_frame, bg='white', highlightthickness=0, height=50)
self.header_canvas.grid(row=0, column=0, sticky='ew')
self.header_canvas.grid_propagate(False)
self.update_header("No Table Selected")
# Content Area
content = tk.Frame(main_frame, bg='white')
content.grid(row=1, column=0, sticky='nsew')
content.grid_rowconfigure(0, weight=1)
content.grid_columnconfigure(0, weight=1)
self.content = content
# Spacer Frame inside Content to hold Add and Delete buttons
self.spacer = tk.Frame(content, width=100, bg='white')
self.spacer.grid(row=0, column=0, sticky='nw')
self.spacer.grid_propagate(False)
# Treeview wraps in its own frame to manage layout
self.tree_wrapper = tk.Frame(content, bg='white')
self.tree_wrapper.grid(row=0, column=1, sticky='nsew')
self.tree_wrapper.grid_rowconfigure(0, weight=1)
self.tree_wrapper.grid_columnconfigure(0, weight=1)
self._create_buttons()
Spacer and Buttons:
Moved the spacer into the content frame to ensure that the Add and Delete buttons are appropriately positioned within the main content area without overlapping with the sidebar.
The spacer holds the Add (+) and Delete (-) buttons, which appear when a row is selected in the Treeview.
python
def _create_buttons(self) -> None:
"""Create add and delete buttons within the spacer."""
style = ttk.Style()
style.configure("Square.TButton",
font=('Arial', 14),
padding=(1, 1),
width=2,
background='white',
borderwidth=0,
relief='flat')
style.map("Square.TButton",
background=[('active', '#EAEDEF')])
self.add_button = ttk.Button(
self.spacer,
text="+",
command=self.add_entry,
style="Square.TButton"
)
self.delete_button = ttk.Button(
self.spacer,
text="-",
command=self.delete_entry,
style="Square.TButton"
)
# Initially hide buttons until a row is selected
self.add_button.place_forget()
self.delete_button.place_forget()
Treeview Placement:
The Treeview is now placed inside a tree_wrapper frame within the content area to ensure it stretches appropriately and doesn't interfere with the spacer containing the Add/Delete buttons.
python
def setup_treeview(self, columns: list, headings: list) -> None:
"""Set up the Treeview widget with specified columns and headings."""
self.tree = ttk.Treeview(
self.tree_wrapper,
columns=columns,
show='headings',
selectmode='browse',
style="Custom.Treeview"
)
for col, head in zip(columns, headings):
self.tree.heading(col, text=head, anchor='center')
anchor = 'w' if head.lower() == 'notes' else 'center'
width = 350
self.tree.column(col, width=width, anchor=anchor)
self.scrollbar = ttk.Scrollbar(
self.tree_wrapper,
orient="vertical",
command=self.tree.yview,
style="Custom.Vertical.TScrollbar"
)
self.tree.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side='right', fill='y')
self.tree.pack(fill='both', expand=True)
self.tree.bind('<<TreeviewSelect>>', self.on_row_select)
self.tree.bind('<Button-1>', self.on_single_click)
self.tree.bind("<Double-Button-1>", self.on_double_click)
self.tree.bind("<Motion>", self.treeview_click_cursor)
Additional Adjustments:
Error Handling in create_table: Added messagebox.showerror calls to inform the user about errors like empty table names or duplicate table names.
Consistency in Widget Placement: Ensured that all widgets adjust correctly within the new grid layout, preventing overlaps and ensuring that the sidebar remains fixed while the header and content adjust dynamically.