Unmuddled Spectrum Weaver Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog, messagebox
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import seaborn as sns
import os

class DataAnalysisApp:
    def __init__(self, master):
        self.master = master
        master.title("Unmuddled Spectrum Weaver - Data Analysis Tool")
        master.geometry("800x600")

        self.notebook = ttk.Notebook(master)
        self.notebook.pack(expand=True, fill='both', padx=10, pady=10)

        # --- Data Import Tab --- #
        self.import_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.import_tab, text='Data Import')

        self.import_label = ttk.Label(self.import_tab, text="Choose a CSV or Excel file to analyze:")
        self.import_label.pack(pady=10)

        self.browse_button = ttk.Button(self.import_tab, text="Browse", command=self.load_data)
        self.browse_button.pack(pady=5)

        self.file_path = tk.StringVar()
        self.file_path_label = ttk.Label(self.import_tab, textvariable=self.file_path)
        self.file_path_label.pack(pady=5)

        self.data = None

        # --- Data Visualization Tab --- #
        self.visualization_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.visualization_tab, text='Data Visualization')

        self.vis_label = ttk.Label(self.visualization_tab, text="Select columns for visualization:")
        self.vis_label.pack(pady=10)

        self.x_col_label = ttk.Label(self.visualization_tab, text="X-axis Column:")
        self.x_col_label.pack()
        self.x_col_var = tk.StringVar()
        self.x_col_dropdown = ttk.Combobox(self.visualization_tab, textvariable=self.x_col_var, state='readonly')
        self.x_col_dropdown.pack()

        self.y_col_label = ttk.Label(self.visualization_tab, text="Y-axis Column:")
        self.y_col_label.pack()
        self.y_col_var = tk.StringVar()
        self.y_col_dropdown = ttk.Combobox(self.visualization_tab, textvariable=self.y_col_var, state='readonly')
        self.y_col_dropdown.pack()

        self.plot_button = ttk.Button(self.visualization_tab, text="Generate Scatter Plot", command=self.generate_scatter_plot)
        self.plot_button.pack(pady=10)

        self.plot_frame = tk.Frame(self.visualization_tab)
        self.plot_frame.pack(expand=True, fill='both')

        # --- Data Summary Tab --- #
        self.summary_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.summary_tab, text='Data Summary')

        self.summary_text = tk.Text(self.summary_tab, wrap=tk.WORD)
        self.summary_text.pack(expand=True, fill='both', padx=10, pady=10)

    def load_data(self):
        file_path = filedialog.askopenfilename(filetypes=[("CSV Files", "*.csv"), ("Excel Files", "*.xlsx;*.xls")])
        if file_path:
            try:
                if file_path.endswith('.csv'):
                    self.data = pd.read_csv(file_path)
                else:
                    self.data = pd.read_excel(file_path)
                self.file_path.set(file_path)
                messagebox.showinfo("Success", "Data loaded successfully!")
                self.populate_column_dropdowns()
                self.generate_data_summary()

            except Exception as e:
                messagebox.showerror("Error", f"Error loading data: {e}")

    def populate_column_dropdowns(self):
        if self.data is not None:
            columns = list(self.data.columns)
            self.x_col_dropdown['values'] = columns
            self.y_col_dropdown['values'] = columns

    def generate_scatter_plot(self):
        if self.data is not None and self.x_col_var.get() and self.y_col_var.get():
            try:
                x_col = self.x_col_var.get()
                y_col = self.y_col_var.get()

                plt.clf()
                fig, ax = plt.subplots(figsize=(8, 6))
                sns.scatterplot(x=x_col, y=y_col, data=self.data, ax=ax)
                ax.set_title(f'{y_col} vs {x_col}')
                ax.set_xlabel(x_col)
                ax.set_ylabel(y_col)
                ax.grid(True)

                # Embed the plot in the Tkinter window
                canvas = FigureCanvasTkAgg(fig, master=self.plot_frame)
                canvas.draw()
                canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

            except Exception as e:
                messagebox.showerror("Error", f"Error generating plot: {e}")
        else:
            messagebox.showinfo("Info", "Please select both X and Y columns.")

    def generate_data_summary(self):
        if self.data is not None:
            try:
                summary = self.data.describe().to_string()
                self.summary_text.delete("1.0", tk.END)
                self.summary_text.insert(tk.END, summary)
            except Exception as e:
                messagebox.showerror("Error", f"Error generating summary: {e}")

root = tk.Tk()
app = DataAnalysisApp(root)
root.mainloop()
👁️ Viewed: 6

Comments