Commit bcea7f82 authored by Francesco Amadori's avatar Francesco Amadori
Browse files

HelpButton.py

parent 091aa6eb
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ class DataReduction:
        # Window
        m = get_monitors()
        self.width = int(m[0].width * 0.95)
        self.height = int(m[0].height * 0.9)
        self.height = int(m[0].height * 0.8)
        self.displace_x = int(m[0].width * 0.04)
        self.displace_y = int(m[0].height * 0.04)
        self.path_default = path_default
+209 −1

File changed.

Preview size limit exceeded, changes collapsed.

+183 −0
Original line number Diff line number Diff line
"""
HelpButton - A reusable help button widget that displays information in a popup.

This module provides a help button with a blue electric background and a yellow-gold
question mark that opens a popup with detailed descriptions when clicked.
"""

import tkinter as tk
from tkinter import ttk


class HelpButton:
    """
    A help button widget that displays information in a popup window.

    This button has a blue electric background with a yellow-gold question mark.
    When clicked, it opens a popup window displaying help text.
    """

    def __init__(self, parent, row, column, title, help_text, columnspan=1, rowspan=1, **kwargs):
        """
        Initialize the HelpButton.

        Args:
            parent: Parent widget
            row: Grid row position
            column: Grid column position
            title: Title of the popup window
            help_text: Dictionary with parameter names as keys and descriptions as values
            columnspan: Number of columns to span (default: 1)
            rowspan: Number of rows to span (default: 1)
            **kwargs: Additional keyword arguments for grid configuration
        """
        self.parent = parent
        self.title = title
        self.help_text = help_text

        # Create the button with blue electric background and yellow-gold question mark
        self.button = tk.Button(
            parent,
            text="?",
            bg="#0080FF",  # Electric blue background
            fg="#FFD700",  # Yellow-gold text
            font=("Arial", 16, "bold"),
            width=3,
            height=1,
            relief=tk.RAISED,
            borderwidth=3,
            command=self.show_help_popup
        )

        # Grid the button
        self.button.grid(row=row, column=column, columnspan=columnspan, rowspan=rowspan, padx=2, pady=2, **kwargs)

        # Add hover effect
        self.button.bind("<Enter>", self._on_enter)
        self.button.bind("<Leave>", self._on_leave)

    def _on_enter(self, event):
        """Change button appearance on mouse enter."""
        self.button.config(bg="#0099FF", relief=tk.SUNKEN)

    def _on_leave(self, event):
        """Restore button appearance on mouse leave."""
        self.button.config(bg="#0080FF", relief=tk.RAISED)

    def show_help_popup(self):
        """Show the help popup window with the help text."""
        # Create popup window
        popup = tk.Toplevel(self.parent)
        popup.title(self.title)
        popup.geometry("700x600")
        popup.configure(bg="white")

        # Make popup modal
        popup.transient(self.parent)
        popup.grab_set()

        # Create main frame with scrollbar
        main_frame = ttk.Frame(popup)
        main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # Create canvas and scrollbar
        canvas = tk.Canvas(main_frame, bg="white")
        scrollbar = ttk.Scrollbar(main_frame, orient=tk.VERTICAL, command=canvas.yview)
        scrollable_frame = ttk.Frame(canvas)

        scrollable_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(scrollregion=canvas.bbox("all"))
        )

        canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
        canvas.configure(yscrollcommand=scrollbar.set)

        # Add title label
        title_label = tk.Label(
            scrollable_frame,
            text=self.title,
            font=("Arial", 18, "bold"),
            bg="white",
            fg="#0080FF"
        )
        title_label.pack(pady=(0, 20))

        # Add separator
        separator = ttk.Separator(scrollable_frame, orient=tk.HORIZONTAL)
        separator.pack(fill=tk.X, pady=(0, 20))

        # Add help text entries
        for param_name, description in self.help_text.items():
            # Parameter name frame
            param_frame = tk.Frame(scrollable_frame, bg="white")
            param_frame.pack(fill=tk.X, pady=10, padx=10)

            # Parameter name label
            param_label = tk.Label(
                param_frame,
                text=param_name,
                font=("Arial", 12, "bold"),
                bg="white",
                fg="#0080FF",
                anchor="w"
            )
            param_label.pack(anchor="w")

            # Description label
            desc_label = tk.Label(
                param_frame,
                text=description,
                font=("Arial", 10),
                bg="white",
                fg="black",
                anchor="w",
                justify=tk.LEFT,
                wraplength=650
            )
            desc_label.pack(anchor="w", padx=(20, 0), pady=(5, 0))

            # Add separator between entries
            entry_separator = ttk.Separator(scrollable_frame, orient=tk.HORIZONTAL)
            entry_separator.pack(fill=tk.X, pady=(10, 0))

        # Pack canvas and scrollbar
        canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        # Add close button at the bottom
        button_frame = tk.Frame(popup, bg="white")
        button_frame.pack(fill=tk.X, padx=10, pady=10)

        close_button = tk.Button(
            button_frame,
            text="Close",
            font=("Arial", 12, "bold"),
            bg="#0080FF",
            fg="white",
            width=15,
            height=2,
            command=popup.destroy
        )
        close_button.pack()

        # Center the popup window
        popup.update_idletasks()
        x = (popup.winfo_screenwidth() // 2) - (popup.winfo_width() // 2)
        y = (popup.winfo_screenheight() // 2) - (popup.winfo_height() // 2)
        popup.geometry(f"+{x}+{y}")

        # Bind mouse wheel scrolling
        def _on_mousewheel(event):
            canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")

        canvas.bind_all("<MouseWheel>", _on_mousewheel)
        popup.bind("<Destroy>", lambda e: canvas.unbind_all("<MouseWheel>"))

    def grid_remove(self):
        """Remove the button from the grid."""
        self.button.grid_remove()

    def grid(self, **kwargs):
        """Grid the button with new parameters."""
        self.button.grid(**kwargs)
 No newline at end of file