on
news
- Get link
- X
- Other Apps
import numpy as np
import tkinter as tk
from tkinter import ttk, messagebox
def calculate_statistics():
try:
data = [float(x) for x in entry_data.get().split(',')]
data_np = np.array(data)
mean = np.mean(data_np)
median = np.median(data_np)
std_dev = np.std(data_np)
minimum = np.min(data_np)
maximum = np.max(data_np)
result_text = f"Mean: {mean:.2f}\n" \
f"Median: {median:.2f}\n" \
f"Standard Deviation: {std_dev:.2f}\n" \
f"Minimum: {minimum:.2f}\n" \
f"Maximum: {maximum:.2f}"
text_result.delete("1.0", tk.END) # Clear previous results
text_result.insert(tk.END, result_text)
except ValueError:
messagebox.showerror("Error", "Invalid input. Please enter numbers separated by commas.")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
# --- GUI Setup ---
root = tk.Tk()
root.title("Simple Statistics Calculator")
# Input Label and Entry
label_data = ttk.Label(root, text="Enter numbers (comma-separated):")
label_data.pack(pady=5)
entry_data = ttk.Entry(root, width=40)
entry_data.pack(pady=5)
# Calculate Button
button_calculate = ttk.Button(root, text="Calculate", command=calculate_statistics)
button_calculate.pack(pady=10)
# Result Text Area
label_result = ttk.Label(root, text="Results:")
label_result.pack(pady=5)
text_result = tk.Text(root, height=10, width=40)
text_result.pack(pady=5)
root.mainloop()
-This Python script creates a simple statistics calculator using the NumPy library and a graphical user interface (GUI) built with Tkinter. Here's a breakdown of the code:
How to Run on Windows:
pip install numpy
(Tkinter usually comes pre-installed with Python on Windows, but if not, you might need to install the python-tk package.)
python statistics_calculator.py
This will open a window with the statistics calculator. Enter numbers separated by commas, click "Calculate," and the results will be displayed in the text area.
Comments
Post a Comment