MetaMorph Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
import subprocess
import json
import re

class MetaMorphApp:
    def __init__(self, master):
        self.master = master
        master.title("MetaMorph: The Multi-Tool")
        master.geometry("600x400")

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

        self.create_text_analysis_tab()
        self.create_file_converter_tab()
        self.create_network_tools_tab()
        self.create_details_button()

    def create_text_analysis_tab(self):
        self.text_analysis_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.text_analysis_tab, text="Text Analysis")

        self.text_label = tk.Label(self.text_analysis_tab, text="Enter Text:")
        self.text_label.pack(pady=5)

        self.text_input = tk.Text(self.text_analysis_tab, height=5, width=50)
        self.text_input.pack(pady=5)

        self.analyze_button = tk.Button(self.text_analysis_tab, text="Analyze Text", command=self.analyze_text)
        self.analyze_button.pack(pady=5)

        self.analysis_output = tk.Text(self.text_analysis_tab, height=5, width=50, state=tk.DISABLED)
        self.analysis_output.pack(pady=5)

    def analyze_text(self):
        text = self.text_input.get("1.0", tk.END).strip()

        if not text:
            messagebox.showinfo("Info", "Please enter some text.")
            return

        word_count = len(text.split())
        char_count = len(text)
        sentences = re.split(r'[.!?]+', text)
        sentence_count = len([s for s in sentences if s.strip()]) # Avoid empty strings

        analysis_result = f"Word Count: {word_count}\nCharacter Count: {char_count}\nSentence Count: {sentence_count}"

        self.analysis_output.config(state=tk.NORMAL)
        self.analysis_output.delete("1.0", tk.END)
        self.analysis_output.insert(tk.END, analysis_result)
        self.analysis_output.config(state=tk.DISABLED)


    def create_file_converter_tab(self):
        self.file_converter_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.file_converter_tab, text="File Converter")

        self.input_file_label = tk.Label(self.file_converter_tab, text="Input File:")
        self.input_file_label.pack(pady=5)

        self.input_file_path = tk.StringVar()
        self.input_file_entry = tk.Entry(self.file_converter_tab, textvariable=self.input_file_path, width=40)
        self.input_file_entry.pack(pady=5)

        self.browse_button = tk.Button(self.file_converter_tab, text="Browse", command=self.browse_file)
        self.browse_button.pack(pady=5)

        self.convert_to_label = tk.Label(self.file_converter_tab, text="Convert to:")
        self.convert_to_label.pack(pady=5)

        self.convert_to_options = ["txt", "pdf", "json", "csv"]
        self.convert_to_variable = tk.StringVar(value=self.convert_to_options[0])
        self.convert_to_dropdown = tk.OptionMenu(self.file_converter_tab, self.convert_to_variable, *self.convert_to_options)
        self.convert_to_dropdown.pack(pady=5)

        self.convert_button = tk.Button(self.file_converter_tab, text="Convert File", command=self.convert_file)
        self.convert_button.pack(pady=5)

        self.conversion_output = tk.Text(self.file_converter_tab, height=3, width=50, state=tk.DISABLED)
        self.conversion_output.pack(pady=5)

    def browse_file(self):
        filename = filedialog.askopenfilename()
        self.input_file_path.set(filename)

    def convert_file(self):
        input_file = self.input_file_path.get()
        output_format = self.convert_to_variable.get()

        if not input_file:
            messagebox.showinfo("Info", "Please select an input file.")
            return

        try:
            # Basic conversion logic (can be extended with more robust methods)
            with open(input_file, 'r') as f:
                content = f.read()

            output_file = os.path.splitext(input_file)[0] + "." + output_format

            if output_format == "txt":
                with open(output_file, 'w') as f:
                    f.write(content)
            elif output_format == "json":
                # Attempt to parse as JSON, otherwise, just dump it as a string
                try:
                    data = json.loads(content)
                except json.JSONDecodeError:
                    data = {"content": content}
                with open(output_file, 'w') as f:
                    json.dump(data, f, indent=4)
            elif output_format == 'csv':
                # Simple CSV conversion (more advanced CSV handling is possible)
                lines = content.splitlines()
                with open(output_file, 'w') as f:
                    for line in lines:
                        f.write(line.replace(',', ';') + '\n') # Replace commas with semicolons as a basic example
            elif output_format == 'pdf':
                 # PDF conversion, requires reportlab. Make sure its installed
                try:
                  from reportlab.pdfgen import canvas
                  from reportlab.lib.pagesizes import letter

                  c = canvas.Canvas(output_file, pagesize=letter)
                  textobject = c.beginText()
                  textobject.setTextOrigin(10,730)
                  for line in content.splitlines(False):
                      textobject.textLine(line.rstrip())
                  c.drawText(textobject)
                  c.save()
                except ImportError:
                   messagebox.showinfo("Info", "Reportlab is not installed.  Please install with pip install reportlab")
                   return


            self.conversion_output.config(state=tk.NORMAL)
            self.conversion_output.delete("1.0", tk.END)
            self.conversion_output.insert(tk.END, f"File converted to {output_file}")
            self.conversion_output.config(state=tk.DISABLED)

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

    def create_network_tools_tab(self):
        self.network_tools_tab = ttk.Frame(self.notebook)
        self.notebook.add(self.network_tools_tab, text="Network Tools")

        self.host_label = tk.Label(self.network_tools_tab, text="Enter Hostname or IP:")
        self.host_label.pack(pady=5)

        self.host_input = tk.Entry(self.network_tools_tab, width=30)
        self.host_input.pack(pady=5)

        self.ping_button = tk.Button(self.network_tools_tab, text="Ping", command=self.ping_host)
        self.ping_button.pack(pady=5)

        self.ping_output = tk.Text(self.network_tools_tab, height=5, width=50, state=tk.DISABLED)
        self.ping_output.pack(pady=5)

    def ping_host(self):
        host = self.host_input.get()
        if not host:
            messagebox.showinfo("Info", "Please enter a hostname or IP address.")
            return

        try:
            # Use subprocess to run the ping command (cross-platform)
            process = subprocess.Popen(['ping', '-c', '3', host],  # -c 3: send 3 packets
                                       stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout, stderr = process.communicate()

            # Decode the output
            output = stdout.decode('utf-8')
            error = stderr.decode('utf-8')

            self.ping_output.config(state=tk.NORMAL)
            self.ping_output.delete("1.0", tk.END)

            if error:
                self.ping_output.insert(tk.END, f"Error: {error}")
            else:
                self.ping_output.insert(tk.END, output)

            self.ping_output.config(state=tk.DISABLED)

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

    def create_details_button(self):
      self.details_button = tk.Button(self.master, text="Details", command=self.show_details)
      self.details_button.pack(pady=10)

    def show_details(self):
        details_text = "MetaMorph is a versatile multi-tool designed to simplify common tasks. It includes text analysis capabilities, allowing users to analyze text statistics.  A file converter supports conversions between txt, pdf, json and csv formats.  Finally, it includes basic network tools, such as a ping utility, for basic network diagnostics.  Future updates will bring additional features and refinements."
        messagebox.showinfo("MetaMorph Details", details_text)


root = tk.Tk()
app = MetaMorphApp(root)
root.mainloop()
👁️ Viewed: 9

Comments