Generation

generate functionSat, 21 Dec 2024

crea una venta con una grafica con botones para dibujar en 3d

Aquí te muestro un ejemplo de cómo crear una venta con una gráfica que permite dibujar en 3D utilizando la biblioteca `matplotlib` y `tkinter` en Python. ```python import tkinter as tk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import numpy as np class Dibujador3D: def __init__(self, root): self.root = root self.root.title("Dibujador 3D") self.figure = Figure(figsize=(6, 6), dpi=100) self.ax = self.figure.add_subplot(111, projection='3d') self.canvas = FigureCanvasTkAgg(self.figure, master=root) self.canvas.draw() self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) self.x = np.linspace(-10, 10, 100) self.y = np.linspace(-10, 10, 100) self.x, self.y = np.meshgrid(self.x, self.y) self.z = np.sin(np.sqrt(self.x**2 + self.y**2)) self.ax.plot_surface(self.x, self.y, self.z, cmap='viridis') self.canvas.draw() self.button_frame = tk.Frame(root) self.button_frame.pack(side=tk.BOTTOM) self.clear_button = tk.Button(self.button_frame, text="Clear", command=self.clear_dibujo) self.clear_button.pack(side=tk.LEFT) self.line_button = tk.Button(self.button_frame, text="Dibujar línea", command=self.dibujar_linea) self.line_button.pack(side=tk.LEFT) self.circle_button = tk.Button(self.button_frame, text="Dibujar círculo", command=self.dibujar_circulo) self.circle_button.pack(side=tk.LEFT) self.sphere_button = tk.Button(self.button_frame, text="Dibujar esfera", command=self.dibujar_esfera) self.sphere_button.pack(side=tk.LEFT) def clear_dibujo(self): self.ax.clear() self.canvas.draw() def dibujar_linea(self): self.ax.plot([0, 10], [0, 0], [0, 0], 'r') self.canvas.draw() def dibujar_circulo(self): self.ax.plot(np.cos(np.linspace(0, 2*np.pi, 100)), np.sin(np.linspace(0, 2*np.pi, 100)), np.zeros(100), 'g') self.canvas.draw() def dibujar_esfera(self): u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = 10 * np.outer(np.cos(u), np.sin(v)) y = 10 * np.outer(np.sin(u), np.sin(v)) z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) self.ax.plot_surface(x, y, z, cmap='viridis') self.canvas.draw() if __name__ == "__main__": root = tk.Tk() dibujador = Dibujador3D(root) root.mainloop() ``` Este código crea una venta con una gráfica 3D que permite dibujar líneas, círculos y esferas. Los botones en la parte inferior de la venta permiten realizar estas acciones. La gráfica se actualiza en tiempo real después de cada acción. Recuerda que debes tener instalada la biblioteca `matplotlib` para ejecutar este código. Puedes instalarla con pip: ```bash pip install matplotlib ```

Want to kickstart your project?Use the new AI Studio to create your code