142 lines
5.6 KiB
Python
142 lines
5.6 KiB
Python
|
"""
|
||
|
Implementación básica librería TKinter
|
||
|
"""
|
||
|
|
||
|
from tkinter import ttk
|
||
|
from tkinter import *
|
||
|
|
||
|
import sqlite3
|
||
|
|
||
|
class Producto:
|
||
|
|
||
|
db = 'basededatos.db'
|
||
|
|
||
|
def __init__(self, ventana):
|
||
|
self.vent = ventana
|
||
|
self.vent.title('Pro-LaP C \'Inventary\' ')
|
||
|
|
||
|
# Creacion del contenedor del frame
|
||
|
frame = LabelFrame(self.vent, text='Registrar nuevo artículo')
|
||
|
frame.grid(row=0, column=0, columnspan=3, pady=20)
|
||
|
|
||
|
# Entrada de Artículo
|
||
|
Label(frame, text='Artículo :').grid(row=1, column=0)
|
||
|
self.articulo = Entry(frame)
|
||
|
self.articulo.grid(row=1, column=1)
|
||
|
self.articulo.focus()
|
||
|
# Entrada Cantidad
|
||
|
Label(frame, text='Cantidad :').grid(row=2, column=0)
|
||
|
self.cantidad = Entry(frame)
|
||
|
self.cantidad.grid(row=2, column=1)
|
||
|
|
||
|
# Boton agregar Articulo
|
||
|
ttk.Button(frame, text='Añadir a Inventario', command=self.add_articulo).grid(row=3, columnspan=2, sticky=W+E)
|
||
|
# Boton Borrar Articulo
|
||
|
ttk.Button(text='Eliminar', command=self.del_articulo).grid(row=5, column=0, sticky=W+E)
|
||
|
# Boton Actualizar Articulo
|
||
|
ttk.Button(text='Editar', command=self.edit_articulo).grid(row=5, column=1, sticky=W+E)
|
||
|
|
||
|
# Mensajes de Informacion
|
||
|
self.info = Label(text='', fg='red')
|
||
|
self.info.grid(row=3, column=0, columnspan=2, sticky=W+E)
|
||
|
|
||
|
# Tabla
|
||
|
self.tree = ttk.Treeview(height=10, columns=2)
|
||
|
self.tree.grid(row=4, column=0, columnspan=2)
|
||
|
self.tree.heading('#0', text='Artículo', anchor=CENTER)
|
||
|
self.tree.heading('#1', text='Cantidad', anchor=CENTER)
|
||
|
|
||
|
self.get_articulos()
|
||
|
|
||
|
def consultar(self, query, parametros=()):
|
||
|
with sqlite3.connect(self.db) as conn:
|
||
|
cursor = conn.cursor()
|
||
|
respuesta = cursor.execute(query, parametros)
|
||
|
conn.commit()
|
||
|
return respuesta
|
||
|
|
||
|
def get_articulos(self):
|
||
|
#Limpiando el Tree
|
||
|
records = self.tree.get_children()
|
||
|
for elemento in records:
|
||
|
self.tree.delete(elemento)
|
||
|
#consulta
|
||
|
query = 'SELECT * FROM articulos ORDER BY id DESC'
|
||
|
db_rows = self.consultar(query)
|
||
|
for row in db_rows:
|
||
|
self.tree.insert('', 0, text=row[1], values=row[2])
|
||
|
|
||
|
def validacion(self):
|
||
|
return len(self.articulo.get()) != 0 and len(self.cantidad.get()) != 0
|
||
|
|
||
|
def add_articulo(self):
|
||
|
if self.validacion():
|
||
|
#print(self.articulo.get())
|
||
|
#print(self.cantidad.get())
|
||
|
query = 'INSERT INTO articulos VALUES(NULL, ?, ?)'
|
||
|
parametros = (self.articulo.get(), self.cantidad.get())
|
||
|
self.consultar(query, parametros)
|
||
|
self.info['text'] = 'Artículo agregado {}'.format(self.articulo.get())
|
||
|
self.articulo.delete(0, END)
|
||
|
self.cantidad.delete(0, END)
|
||
|
else:
|
||
|
self.info['text'] = 'Articulo \"Y\" Cantidad requeridos'
|
||
|
self.get_articulos()
|
||
|
|
||
|
def del_articulo(self):
|
||
|
self.info['text']=''
|
||
|
try:
|
||
|
self.tree.item(self.tree.selection())['text'][0]
|
||
|
except IndexError as e:
|
||
|
self.info['text'] = 'Selcciona el Artículo a Eliminar'
|
||
|
return
|
||
|
self.info['text'] = ''
|
||
|
item_sel=self.tree.item(self.tree.selection())['text']
|
||
|
query = 'DELETE FROM articulos WHERE nombre = ?'
|
||
|
self.consultar(query, (item_sel, ))
|
||
|
self.info['text'] = 'Artículo Eliminado: {}'.format(item_sel)
|
||
|
self.get_articulos()
|
||
|
|
||
|
def edit_articulo(self):
|
||
|
self.info['text'] = ''
|
||
|
try:
|
||
|
self.tree.item(self.tree.selection())['text'][0]
|
||
|
except IndexError as e:
|
||
|
self.info['text'] = 'Selcciona el Artículo a Editar'
|
||
|
return
|
||
|
old_articulo = self.tree.item(self.tree.selection())['text']
|
||
|
old_cantidad = self.tree.item(self.tree.selection())['values'][0]
|
||
|
self.vent_edit = Toplevel()
|
||
|
self.vent_edit.title = 'Editar Artículo'
|
||
|
|
||
|
#Articulo Antiguo
|
||
|
Label(self.vent_edit, text='Articulo Original :').grid(row=0, column=1)
|
||
|
Entry(self.vent_edit, textvariable=StringVar(self.vent_edit, value=old_articulo), state='readonly').grid(row=0, column=2)
|
||
|
#Nuevo Artículo
|
||
|
Label(self.vent_edit, text='Nuevo Artículo :').grid(row=1, column=1)
|
||
|
nuevo_art = Entry(self.vent_edit)
|
||
|
nuevo_art.grid(row=1, column=2)
|
||
|
#Cantidad Antigua
|
||
|
Label(self.vent_edit, text='Cantidad Original :').grid(row=2, column=1)
|
||
|
Entry(self.vent_edit, textvariable=StringVar(self.vent_edit, value=old_cantidad), state='readonly').grid(row=2, column=2)
|
||
|
#Nueva Cantidad
|
||
|
Label(self.vent_edit, text='Nueva Cantidad :').grid(row=3, column=1)
|
||
|
nueva_cant = Entry(self.vent_edit)
|
||
|
nueva_cant.grid(row=3, column=2)
|
||
|
#Boton Actualizar
|
||
|
Button(self.vent_edit, text='Actualizar', command=lambda: self.editar_art(nuevo_art.get(), nueva_cant.get(), old_articulo, old_cantidad)).grid(row=4,column=2, sticky=W)
|
||
|
|
||
|
def editar_art(self, nuevo_art, nueva_cant, old_articulo, old_cantidad):
|
||
|
query = 'UPDATE articulos SET nombre=?, cantidad=? WHERE nombre=? AND cantidad=?'
|
||
|
parametros = (nuevo_art, nueva_cant, old_articulo, old_cantidad)
|
||
|
self.consultar(query, parametros)
|
||
|
self.vent_edit.destroy()
|
||
|
self.info['text'] = 'Entrada Actualizada :{} '.format(nuevo_art)
|
||
|
self.get_articulos()
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
ventana= Tk()
|
||
|
prlpc = Producto(ventana)
|
||
|
ventana.resizable(0,0)
|
||
|
ventana.mainloop()
|