|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk, messagebox |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
| 5 | +class PlotApp: |
| 6 | + def __init__(self, root): |
| 7 | + self.root = root |
| 8 | + self.root.title("Plot Generator") |
| 9 | + self.root.geometry("400x400") |
| 10 | + self.root.resizable(False, False) |
| 11 | + |
| 12 | + tk.Label(root, text="Select Plot Type:", font=("Arial", 12)).pack(pady=10) |
| 13 | + |
| 14 | + self.plot_type = tk.StringVar() |
| 15 | + self.plot_dropdown = ttk.Combobox(root, textvariable=self.plot_type, state="readonly", font=("Arial", 11)) |
| 16 | + self.plot_dropdown['values'] = ("Line Plot", "Scatter Plot") |
| 17 | + self.plot_dropdown.current(0) |
| 18 | + self.plot_dropdown.pack(pady=5) |
| 19 | + |
| 20 | + tk.Label(root, text="Enter X values (comma-separated):", font=("Arial", 11)).pack(pady=10) |
| 21 | + self.x_entry = tk.Entry(root, font=("Arial", 11), width=40) |
| 22 | + self.x_entry.pack() |
| 23 | + |
| 24 | + tk.Label(root, text="Enter Y values (comma-separated):", font=("Arial", 11)).pack(pady=10) |
| 25 | + self.y_entry = tk.Entry(root, font=("Arial", 11), width=40) |
| 26 | + self.y_entry.pack() |
| 27 | + |
| 28 | + tk.Button(root, text="Generate Plot", command=self.generate_plot, bg="#2196F3", fg="white", font=("Arial", 12)).pack(pady=20) |
| 29 | + |
| 30 | + def generate_plot(self): |
| 31 | + try: |
| 32 | + x_vals = list(map(float, self.x_entry.get().split(","))) |
| 33 | + y_vals = list(map(float, self.y_entry.get().split(","))) |
| 34 | + |
| 35 | + if len(x_vals) != len(y_vals): |
| 36 | + raise ValueError("X and Y must have the same number of values") |
| 37 | + |
| 38 | + plt.figure(figsize=(6,4)) |
| 39 | + if self.plot_type.get() == "Line Plot": |
| 40 | + plt.plot(x_vals, y_vals, marker='o') |
| 41 | + else: |
| 42 | + plt.scatter(x_vals, y_vals) |
| 43 | + |
| 44 | + plt.title(self.plot_type.get()) |
| 45 | + plt.xlabel("X values") |
| 46 | + plt.ylabel("Y values") |
| 47 | + plt.grid(True) |
| 48 | + plt.tight_layout() |
| 49 | + plt.show() |
| 50 | + |
| 51 | + except Exception as e: |
| 52 | + messagebox.showerror("Error", f"Invalid input: {e}") |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + root = tk.Tk() |
| 56 | + app = PlotApp(root) |
| 57 | + root.mainloop() |
0 commit comments