Split the "..." menu into for buttons "S" for stats, "I" for import, "E" for Export, "D" for change datatypes. lets think step by step. Python 3.12 Code: class Header:
"""Class for handling header UI."""
To split the "..." menu into four buttons "S" for stats, "I" for import, "E" for export, and "D" for change datatypes, we'll need to modify the _setup_settings_section method in your Header class. We'll replace the "..." label and menu with four buttons and connect each button to its respective function.
Here's how you can do it step by step:
Remove the Existing Settings Label and Menu:
In the _setup_settings_section method, remove the code that creates the settings_label and binds the menu to it.
Create Four Buttons:
We'll create four buttons labeled "S", "I", "E", and "D" and place them in the settings_container.
Define the show_stats Method:
Since we previously used a menu for stats, we'll define a new method called show_stats to handle displaying stats when the "S" button is clicked.
Update Hover Effects:
Add hover effects for the new buttons to match the rest of your application's style.
Here is the modified code for your Header class:
python
class Header:
"""Class for handling header UI."""
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")
# Create four buttons: "S", "I", "E", "D"
button_s = tk.Button(settings_container, text="S", command=self.show_stats,
bg=self.colors["background"], fg=self.colors["text"],
font=self.fonts["text"], cursor="hand2")
button_i = tk.Button(settings_container, text="I", command=self.app.table_io.import_file,
bg=self.colors["background"], fg=self.colors["text"],
font=self.fonts["text"], cursor="hand2")
button_e = tk.Button(settings_container, text="E", command=self.app.table_io.export_table,
bg=self.colors["background"], fg=self.colors["text"],
font=self.fonts["text"], cursor="hand2")
button_d = tk.Button(settings_container, text="D", command=self.show_datatype_dialog,
bg=self.colors["background"], fg=self.colors["text"],
font=self.fonts["text"], cursor="hand2")
# Arrange the buttons in the settings_container
button_s.pack(side=tk.LEFT, padx=5)
button_i.pack(side=tk.LEFT, padx=5)
button_e.pack(side=tk.LEFT, padx=5)
button_d.pack(side=tk.LEFT, padx=5)
# Bind hover effects to buttons
def on_button_enter(event):
event.widget.config(bg=self.colors["button_selected"])
def on_button_leave(event):
event.widget.config(bg=self.colors["background"])
buttons = [button_s, button_i, button_e, button_d]
for button in buttons:
button.bind("<Enter>", on_button_enter)
button.bind("<Leave>", on_button_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 current 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_rows()
# Calculate stats (example: count of True boolean values)
boolean_count = sum(
1 for row in data for cell in row if isinstance(cell, bool) and cell is True
)
# Display the stats in a messagebox
stats_message = f"Boolean Count (True): {boolean_count}"
messagebox.showinfo("Table Stats", stats_message)
def update_true_counter(self, count_text: str) -> None:
pass # This method can be removed or updated if no longer needed
def show_datatype_dialog(self) -> None:
"""Show dropdown menu 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')
self.datatype_menu = tk.Menu(self.root, tearoff=0, bg=self.colors["background"],
fg=self.colors["text"], font=self.fonts["text"])
self.menu_x = self.root.winfo_pointerx()
self.menu_y = self.root.winfo_pointery()
self._populate_datatype_menu(current_headers)
self.datatype_menu.post(self.menu_x, self.menu_y)
def _populate_datatype_menu(self, headers):
"""Populate the datatype menu with headers and their current types."""
self.datatype_menu.delete(0, 'end')
for header in headers:
header_index = headers.index(header)
current_type = self.data_types[header_index]
submenu = tk.Menu(self.datatype_menu, tearoff=0, bg=self.colors["background"],
fg=self.colors["text"], font=self.fonts["text"])
self.datatype_menu.add_cascade(label=f"{header}: {current_type}", menu=submenu)
for dtype in self.DATA_TYPE_OPTIONS:
submenu.add_command(
label=dtype,
command=lambda h=header, t=dtype: self._update_datatype(h, t),
background=self.colors["button_selected"] if dtype == current_type
else self.colors["background"]
)
self.datatype_menu.add_separator()
self.datatype_menu.add_command(label="Apply Changes",
command=self._apply_changes)
def _update_datatype(self, header: str, new_type: str) -> None:
"""Update the datatype for a specific header."""
current_headers = self.app.current_sheet.headers()
header_index = current_headers.index(header)
self.data_types[header_index] = new_type
self._populate_datatype_menu(current_headers)
self.datatype_menu.post(self.menu_x, self.menu_y)
def _apply_changes(self) -> None:
"""Apply the datatype changes and update the table."""
table_name = self.app.current_table_name
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()
self.datatype_menu.unpost()
Explanation of Changes:
Removed the "..." Settings Label and Menu:
We removed the settings_label and the code related to the settings menu since we're replacing it with buttons.
Created Four Buttons:
We added four buttons ("S", "I", "E", "D") in the settings_container and connected them to their respective methods.
Defined show_stats Method:
We added the show_stats method to display stats about the current table. This method currently calculates and displays the count of True boolean values but can be extended to include more stats as needed.
Added Hover Effects:
We added on_button_enter and on_button_leave functions to handle hover effects for the buttons, changing their background color when the mouse pointer enters or leaves.
This modification splits the "..." menu into four separate buttons as per your request, providing a more accessible interface for these actions.
Note: Make sure that all the methods and properties used (like self.app.table_io.import_file, self.app.table_io.export_table, self.show_datatype_dialog, etc.) are correctly implemented in your application. If any methods are missing or need adjustments to work with the new setup, you'll need to update them accordingly.