DATA_TYPE_OPTIONS = ["string", "integer", "float", "boolean"]
def __init__(self, root: tk.Tk, app: 'TableApp'):
self.root = root
self.app = app
self.style = app.style
self.colors = app.colors
self.fonts = app.fonts
self.dimensions = app.dimensions
self.table_name_label = None
self.lower_header_frame = None
self.result = None
self._setup_header()
def _setup_header(self) -> None:
header_frame = self._setup_main_frame()
self._setup_upper_section(header_frame)
self._setup_lower_section(header_frame)
def _setup_main_frame(self) -> tk.Frame:
header_frame = 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)
header_frame.grid_rowconfigure(0, weight=0)
header_frame.grid_rowconfigure(1, weight=0)
for i in range(5):
header_frame.grid_columnconfigure(i, weight=1 if i == 4 else 0)
return header_frame
def _setup_upper_section(self, parent: tk.Frame) -> None:
upper_frame = create_frame(parent=parent, width=None, bg=self.colors["background"])
upper_frame.grid(row=0, column=0, columnspan=6, sticky="ew", pady=(0, 10))
upper_frame.grid_columnconfigure(5, weight=1)
self._add_upper_content(upper_frame)
def _add_upper_content(self, frame: tk.Frame) -> None:
tk.Label(
frame,
text="Data > ",
bg=self.colors["background"],
fg=self.colors["header_info"],
font=self.fonts["header_info"]
).grid(row=0, column=0, padx=22, pady=(22, 0), sticky="nw")
self.table_name_label = tk.Label(
frame,
text=self.app.current_table_name or "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")
self._setup_settings_section(frame)
def _setup_settings_section(self, frame: tk.Frame) -> None:
settings_container = tk.Frame(frame, bg=self.colors["background"])
settings_container.grid(row=0, column=5, padx=(0, 40), pady=(22, 0), sticky="ne")
common_label_options = {
'bg': self.colors["background"],
'fg': self.colors["text"],
'font': self.fonts["text"],
'cursor': "hand2",
}
label_s = tk.Label(settings_container, text="S", **common_label_options)
label_i = tk.Label(settings_container, text="I", **common_label_options)
label_e = tk.Label(settings_container, text="E", **common_label_options)
label_d = tk.Label(settings_container, text="D", **common_label_options)
label_s.pack(side=tk.LEFT, padx=5)
label_i.pack(side=tk.LEFT, padx=5)
label_e.pack(side=tk.LEFT, padx=5)
label_d.pack(side=tk.LEFT, padx=5)
label_s.bind("<Button-1>", lambda e: self.show_stats())
label_i.bind("<Button-1>", lambda e: self.app.table_io.import_file())
label_e.bind("<Button-1>", lambda e: self.app.table_io.export_table())
label_d.bind("<Button-1>", lambda e: self.show_datatype_dialog())
def on_label_enter(event):
event.widget.config(bg=self.colors["button_selected"])
def on_label_leave(event):
event.widget.config(bg=self.colors["background"])
labels = [label_s, label_i, label_e, label_d]
for label in labels:
label.bind("<Enter>", on_label_enter)
label.bind("<Leave>", on_label_leave)
def _setup_lower_section(self, parent: tk.Frame) -> None:
self.lower_header_frame = create_frame(parent=parent, width=None, bg=self.colors["background"])
self.lower_header_frame.grid(row=1, column=0, columnspan=5, sticky="ew")
def show_stats(self):
"""Display stats about the boolean columns in a menu-like popup with a table."""
table_name = self.app.current_table_name
if not table_name:
messagebox.showwarning("No Table Selected", "Please select a table to view stats.")
return
if not self.app.current_sheet:
messagebox.showwarning("No Data", "No data available to show stats.")
return
data = self.app.current_sheet.get_sheet_data()
current_headers = self.app.current_sheet.headers()
data_types = self.app.data_manager.tables_data[table_name].get("data_types", [])
while len(data_types) < len(current_headers):
data_types.append('string')
total_rows = len(data)
boolean_columns = []
for col_idx, dtype in enumerate(data_types):
if dtype == 'boolean':
header = current_headers[col_idx]
column_data = [bool(row[col_idx]) if row[col_idx] is not None else False for row in data]
true_count = sum(1 for val in column_data if val is True)
false_count = sum(1 for val in column_data if val is False)
boolean_columns.append((header, true_count, false_count))
if hasattr(self, 'stats_popup') and self.stats_popup.winfo_exists():
self.stats_popup.destroy()
self.stats_popup = tk.Toplevel(self.root)
self.stats_popup.overrideredirect(True)
border_frame = tk.Frame(self.stats_popup, bg=self.colors["frame_border"])
border_frame.pack(padx=0, pady=0, expand=True, fill=tk.BOTH)
content_frame = tk.Frame(border_frame, bg=self.colors["background"])
content_frame.pack(padx=1, pady=1, expand=True, fill=tk.BOTH)
# Add an inner frame with padding to create space between border and separator
inner_frame = tk.Frame(content_frame, bg=self.colors["background"])
inner_frame.pack(padx=10, pady=10, expand=True, fill=tk.BOTH)
total_label = tk.Label(
inner_frame,
text=f"Number of Rows: {total_rows}",
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["text"]
)
total_label.pack(pady=(0, 5), anchor='center')
separator = tk.Frame(inner_frame, bg=self.colors["separator_menu"], height=1)
separator.pack(fill=tk.X, pady=(0, 5))
if boolean_columns:
table_frame = tk.Frame(inner_frame, bg=self.colors["background"])
table_frame.pack()
headers = ["Column", "True", "False"]
for col_num, header_text in enumerate(headers):
header_label = tk.Label(
table_frame,
text=header_text,
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["text"]
)
header_label.grid(row=0, column=col_num, padx=10, pady=2, sticky='w')
for row_num, (header, true_count, false_count) in enumerate(boolean_columns, start=1):
header_label = tk.Label(
table_frame,
text=header,
bg=self.colors["background"],
fg=self.colors["header_info"],
font=self.fonts["text"]
)
header_label.grid(row=row_num, column=0, padx=10, pady=2, sticky='w')
true_label = tk.Label(
table_frame,
text=str(true_count),
bg=self.colors["background"],
fg=self.colors["header_info"],
font=self.fonts["text"]
)
true_label.grid(row=row_num, column=1, padx=10, pady=2, sticky='w')
false_label = tk.Label(
table_frame,
text=str(false_count),
bg=self.colors["background"],
fg=self.colors["header_info"],
font=self.fonts["text"]
)
false_label.grid(row=row_num, column=2, padx=10, pady=2, sticky='w')
for col_num in range(3):
table_frame.grid_columnconfigure(col_num, weight=1)
else:
no_data_label = tk.Label(
inner_frame,
text="No boolean columns to display.",
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["text"]
)
no_data_label.pack(pady=10)
self.stats_popup.update_idletasks()
popup_width = self.stats_popup.winfo_width()
popup_height = self.stats_popup.winfo_height()
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
menu_x = self.root.winfo_pointerx()
menu_y = self.root.winfo_pointery()
padding = 10
if menu_x + popup_width > screen_width - padding:
menu_x = screen_width - popup_width - padding
if menu_y + popup_height > screen_height - padding:
menu_y = screen_height - popup_height - padding
menu_x = max(padding, menu_x)
menu_y = max(padding, menu_y)
self.stats_popup.geometry(f"+{menu_x}+{menu_y}")
def close_popup(event=None):
if self.stats_popup.winfo_exists():
self.stats_popup.destroy()
self.root.unbind("<Button-1>", self.click_outside_popup)
self.root.unbind("<Configure>", self.window_resized)
def click_outside_popup(event):
if not str(event.widget).startswith(str(self.stats_popup)):
close_popup()
self.click_outside_popup = self.root.bind("<Button-1>", click_outside_popup)
self.window_resized = self.root.bind("<Configure>", lambda e: close_popup())
self.stats_popup.bind("<FocusOut>", close_popup)
self.stats_popup.bind("<Escape>", close_popup)
self.stats_popup.focus_force()
def update_true_counter(self, count_text: str) -> None:
pass
def show_datatype_dialog(self) -> None:
"""Show custom popup window for changing column data types."""
table_name = self.app.current_table_name
if not table_name:
messagebox.showwarning("No Table Selected", "Please select a table to change datatypes.")
return
current_headers = self.app.current_sheet.headers() if self.app.current_sheet else []
self.app.data_manager.headers[table_name] = current_headers
self.data_types = self.app.data_manager.tables_data[table_name].get("data_types", []).copy()
while len(self.data_types) < len(current_headers):
self.data_types.append('string')
if hasattr(self, 'datatype_popup') and self.datatype_popup.winfo_exists():
self.datatype_popup.destroy()
self.datatype_popup = tk.Toplevel(self.root)
self.datatype_popup.overrideredirect(True)
# Create the border frame (unchanged)
border_frame = tk.Frame(self.datatype_popup, bg=self.colors["frame_border"])
border_frame.pack(padx=0, pady=0, expand=True, fill=tk.BOTH)
# Create the content frame (unchanged)
content_frame = tk.Frame(border_frame, bg=self.colors["background"])
content_frame.pack(padx=1, pady=1, expand=True, fill=tk.BOTH)
# Add an inner frame with padding to create space between text and border
inner_frame = tk.Frame(content_frame, bg=self.colors["background"])
inner_frame.pack(padx=10, pady=10, expand=True, fill=tk.BOTH)
self.datatype_vars = {}
for header_index, header in enumerate(current_headers):
current_type = self.data_types[header_index]
# Pack frames into inner_frame instead of content_frame
frame = tk.Frame(inner_frame, bg=self.colors["background"])
frame.pack(fill=tk.X, pady=2)
header_label = tk.Label(
frame,
text=f"{header}:",
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["text"]
)
# Optional: Add padding to the label
header_label.pack(side=tk.LEFT, padx=(0, 5))
var = tk.StringVar(value=current_type)
option_menu = tk.OptionMenu(frame, var, *self.DATA_TYPE_OPTIONS)
option_menu.config(
bg=self.colors["background"],
fg=self.colors["text"],
highlightthickness=0,
relief=tk.FLAT
)
option_menu["menu"].config(bg=self.colors["background"], fg=self.colors["text"])
option_menu.pack(side=tk.RIGHT)
self.datatype_vars[header] = var
separator = tk.Frame(inner_frame, bg=self.colors["separator_menu"], height=1)
separator.pack(fill=tk.X, pady=(5, 5))
apply_label = tk.Label(
inner_frame,
text="Apply Changes",
bg=self.colors["background"],
fg=self.colors["text"],
font=self.fonts["text"],
cursor="hand2"
)
apply_label.pack()
apply_label.bind("<Button-1>", lambda e: self._apply_changes())
def on_label_enter(event):
event.widget.config(bg=self.colors["button_selected"])
def on_label_leave(event):
event.widget.config(bg=self.colors["background"])
apply_label.bind("<Enter>", on_label_enter)
apply_label.bind("<Leave>", on_label_leave)
# Rest of your code remains the same...
# (Including positioning logic and event bindings)
self.datatype_popup.update_idletasks()
popup_width = self.datatype_popup.winfo_width()
popup_height = self.datatype_popup.winfo_height()
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
menu_x = self.root.winfo_pointerx()
menu_y = self.root.winfo_pointery()
padding = 10
if menu_x + popup_width > screen_width - padding:
menu_x = screen_width - popup_width - padding
if menu_y + popup_height > screen_height - padding:
menu_y = screen_height - popup_height - padding
menu_x = max(padding, menu_x)
menu_y = max(padding, menu_y)
self.datatype_popup.geometry(f"+{menu_x}+{menu_y}")
def close_popup(event=None):
if self.datatype_popup.winfo_exists():
self.datatype_popup.destroy()
self.root.unbind("<Button-1>", self.click_outside_popup)
self.root.unbind("<Configure>", self.window_resized)
def click_outside_popup(event):
if not str(event.widget).startswith(str(self.datatype_popup)):
close_popup()
self.click_outside_popup = self.root.bind("<Button-1>", click_outside_popup)
self.window_resized = self.root.bind("<Configure>", lambda e: close_popup())
self.datatype_popup.bind("<FocusOut>", close_popup)
self.datatype_popup.bind("<Escape>", close_popup)
self.datatype_popup.focus_force()
def _apply_changes(self) -> None:
"""Apply the datatype changes and update the table."""
table_name = self.app.current_table_name
current_headers = self.app.current_sheet.headers() if self.app.current_sheet else []
self.data_types = []
for header in current_headers:
dtype = self.datatype_vars[header].get()
self.data_types.append(dtype)
self.app.data_manager.tables_data[table_name]["data_types"] = self.data_types
self.app.data_manager.save_data()
self.app.table_display.apply_column_data_types(self.data_types)
self.app.table_display.process_and_save_current_sheet_data()
self.app.refresh_table()
if hasattr(self, 'datatype_popup') and self.datatype_popup.winfo_exists():
self.datatype_popup.destroy()