def __init__(self, ventana): global mP mP = Producto.Producto() self.ventana = ventana self.ventana.title("Ventana Productos") self.ventana.geometry("500x500") self.ventana.resizable(0,0) #contenedor self.contenedor = LabelFrame(self.ventana, text='Ingresar Nuevo Producto') self.contenedor.grid( row = 0, column = 0, columnspan = 3, padx = 20, pady = 20 ) #label nombre Label(self.contenedor, text='Nombre:').grid( row = 1, column = 0 ) #input nombre self.nombre = Entry(self.contenedor) self.nombre.focus() self.nombre.grid( row = 1, column = 1, padx = 5 ) #label precio Label(self.contenedor, text='Precio:').grid( row = 2, column = 0 ) #input precio self.precio = Entry(self.contenedor) self.precio.grid( row = 2, column = 1 ) #mensaje de salida self.mensaje = Label( text = '', fg = 'red' ) self.mensaje.grid( row = 3, column = 0, columnspan = 2, sticky = W + E ) #tablas self.tabla = ttk.Treeview(height = 10, column = 2) self.tabla.grid(row = 4, column = 0, columnspan = 2, padx = 50) self.tabla.heading('#0', text = 'Nombre', anchor = CENTER) self.tabla.heading('#1', text = 'Precio', anchor = CENTER)
class ControladorEditar: #variable global del modelo global mP mP = Producto.Producto() def editarProducto(self, nombre, nombreNuevo, precioNuevo): #logica de editar un producto en la BD y luego mostrarlo en la tabla en tiempo real fila = mP.editarProducto(nombre, nombreNuevo, precioNuevo) if (fila > 0): ventana = Tk() vP = producto.VentanaProducto(ventana) vP.mensaje['text'] = 'Producto Actualizado Correctamente' else: vP.mensaje['text'] = 'ERROR al actualizar el producto' vP.obtenerDatos() ventana.destroy()
class ControladorProducto: #ventana ventana = Tk() #variable global de la vista del producto global vP vP = producto.VentanaProducto(ventana) #variable global del modelo del producto global mP mP = Producto.Producto() def __init__(self, ventana): #boton agregar producto ttk.Button(vP.contenedor, text='Insertar Producto', command=self.agregarProducto).grid(row=3, columnspan=2, padx=10, pady=10, sticky=W + E) #boton eliminar producto ttk.Button(vP.contenedor, text='Eliminar Producto', command=self.eliminarProducto).grid( row=5, column=1, ) #boton editar producto ttk.Button(vP.contenedor, text='Editar Producto', command=self.modificarProducto).grid(row=5, column=0, padx=12, pady=12, sticky=W + E) vP.obtenerDatos() #funcion que valida que los datos no vengan vacios def validar(self): if (len(vP.nombre.get()) > 0 and len(vP.precio.get()) > 0): return True else: return False #logica para agregar un producto en la BD y actualizar los datos en tiempo real def agregarProducto(self): if (self.validar()): nombre = vP.nombre.get() precio = int(vP.precio.get()) fila = mP.registrarProducto(nombre, precio) if (fila > 0): vP.mensaje['text'] = f'{nombre} insertado correctamente' vP.nombre.delete(0, END) vP.precio.delete(0, END) else: vP.mensaje['text'] = "ERROR al insertar el producto" nombre.delete(0, END) precio.delete(0, END) else: vP.mensaje['text'] = "ERROR no ha ingresado datos" vP.obtenerDatos() #logica para eliminar un producto de la BD y actualizar los datos en tiempo real def eliminarProducto(self): try: nombre = vP.tabla.item(vP.tabla.selection())['text'] fila = mP.eliminarProducto(nombre) if (fila > 0): vP.mensaje['text'] = f'{nombre} eliminado correctamente' else: vP.mensaje['text'] = 'ERROR debe seleccionar un producto' vP.obtenerDatos() except: vP.mensaje['text'] = 'ERROR al eliminar el producto' #funcion que abre la ventana editar def modificarProducto(self): nombre = vP.tabla.item(vP.tabla.selection())['text'] precio = vP.tabla.item(vP.tabla.selection())['values'] vE = editar.VentanaModificar(self.ventana, nombre, precio)