Skip to content
import tkinter as tk
from tkinter import ttk

# Dictionary to hold the event conversion items and their respective multiplier values
multipliers = {
    "Mens 55m -> 60m": 1.0749,
    "Womens 55m -> 60m": 1.0771,
    "200m -> 300m": 1.60,
    "300m -> 400m": 1.3964,
    "400m -> 600m": 1.66,
    "500m -> 400m": 0.758,
    "600m -> 800m": 1.38,
    "1km -> 800m": 0.758,
    "1500m -> 1600m": 1.0737,
    "Mile -> 1600m": 0.9942,
    "3km -> 3200m": 1.0737
}

# Calculate the result based on the user input and the conversion selected
def calculate():
    try:
        value = float(entry.get())
        selected_item = multiplier_var.get()
        multiplier = multipliers[selected_item]
        result = value * multiplier
        result_label.config(text=f"Result: {result}")
    except ValueError:
        result_label.config(text="Please enter a valid number.")
    except KeyError:
        result_label.config(text="Please select a valid conversion.")

# Check if the script is running in a Jupyter Notebook
def is_notebook():
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True   # Jupyter notebook or qtconsole
        elif shell == 'TerminalInteractiveShell':
            return False  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False      # Probably standard Python interpreter

if is_notebook():
    from IPython.display import display
    import ipywidgets as widgets

    # Create an entry widget for user input
    entry_label = widgets.Label("Enter a value:")
    entry = widgets.FloatText()

    # Create a dropdown menu for multipliers
    multiplier_var = widgets.Dropdown(
        options=list(multipliers.keys()),
        value=None,
        description='Conversion:',
    )

    # Create a button to perform the calculation
    calculate_button = widgets.Button(description="Calculate")
    result_label = widgets.Label("Result: ")

    def on_button_clicked(b):
        calculate()

    calculate_button.on_click(on_button_clicked)

    # Display the widgets
    display(entry_label, entry, multiplier_var, calculate_button, result_label)
else:
    # Create the main window
    root = tk.Tk()
    root.title("Track and Field Event Performance Conversion Calculator")

    # Create an entry widget for user input
    entry_label = tk.Label(root, text="Enter a value:")
    entry_label.grid(row=0, column=0, padx=10, pady=10)
    entry = tk.Entry(root)
    entry.grid(row=0, column=1, padx=10, pady=10)

    # Create a dropdown menu for multipliers
    multiplier_var = tk.StringVar()
    multiplier_var.set("Select conversion")

    multiplier_menu = ttk.Combobox(root, textvariable=multiplier_var, values=list(multipliers.keys()))
    multiplier_menu.grid(row=1, column=1, padx=10, pady=10)

    # Create a button to perform the calculation
    calculate_button = tk.Button(root, text="Calculate", command=calculate)
    calculate_button.grid(row=2, column=0, columnspan=2, pady=10)

    # Label to display the result
    result_label = tk.Label(root, text="Result: ")
    result_label.grid(row=3, column=0, columnspan=2, pady=10)

    # Run the application
    root.mainloop()