I made a program that has 3 listbox. Then to add a product I did an if that uses a count of the first 2 listboxes, and an elif of the other. The problem is that that elif is not fulfilled, but is taken by the first if. The idea is that an item is selected from those 3 lists. After that if you select from list 1 or 2, go through one side, and if you select from list 3, go through the other.
# ListsBoxs
self.listbox1 = Listbox(frame, width=35, height=28)
activos = ['Caja', 'Banco', 'Valores a depositar','Moneda extranjera',"Fondo fijo", "Titulos y acciones", "Deudores varios","Deudores por venta","Deudores morosos","Deudores en litigio","Documentos a cobrar", "Concepto pagado poadelantado", "Hipotecas a cobrar","Anticipios proveedores", "Accionistas", "Mercaderias", "Materias primas", "Productos en proceso de elaboracion","Productos terminados", "Rodados", "Instalaciones", "Muebles y utiles", "Inmuebles", "Maquinarias", "Llave de negocio", "Marcas y patentes", "Derechos de autor", "Gastos de organizacion"]
self.listbox1.insert(0, *activos)
self.listbox1.grid(row=3, column=0)
self.listbox2 = Listbox(frame, width=35, height=28)
pasivos = ["Proveedores", "Acreedores varios", "Documentos a pagar", "Intereses a pagar", "Obligaciones negociables", "Prestamos a pagar", "Acreedores prendarios e hipotecarios", "Adelantos en cuenta corriente", "Honorarios a pagar", "Sueldos a pagar", "Anses a pagar", "Retencion impuesto a las ganancias", "Retencion IVA", "Dividendos", "Concepto cobrado por adelantado", "Anticipo de clientes", "Cuentas por pagar", "Previsiones"]
self.listbox2.insert(0, *pasivos)
self.listbox2.grid(row=3, column=1)
self.listbox3 = Listbox(frame, width=35, height=28)
resultados = ['Gastos Luz','Descuento obtenido','Descuento otorgado','Interes otorgado','Interes obtenido', 'Ventas','CMV', 'Alquileres cobrados', 'Comision Cobrada', 'Impuestos', 'Alquileres pagados', 'Sueldos y jornales', 'Gastos generales', 'Comisiones pagadas', 'Publicidad', 'Seguros']
self.listbox3.insert(0, *resultados)
self.listbox3.grid(row=3, column=2)
def add_product(self):
try:
if (rows:= self.listbox1.selection_get() or self.listbox2.selection_get()):
query = 'INSERT INTO balance (id, cuenta, debe, haber) VALUES (?, ?, ?, ?)'
parameters = (self.nro_cuenta.get(), rows, self.price_debe.get(), self.price_haber.get())
self.run_query(query, parameters)
self.price_debe.delete(0, END)
self.price_debe.insert(0, int(0))
self.price_haber.delete(0, END)
self.price_haber.insert(0, int(0))
self.nro_cuenta.delete(0, END)
self.mesagge['text'] = 'Se guardo con exito'
elif (rows:= self.listbox3.selection_get()):
query = 'INSERT INTO balance (id, cuenta, debe, haber) VALUES (?, ?, ?, ?)'
parameters = (self.nro_cuenta.get(), rows, self.price_debe.get(), self.price_haber.get())
self.run_query(query, parameters)
self.price_debe.delete(0, END)
self.price_haber(0, END)
self.nro_cuenta.delete(0, END)
self.mesagge['text'] = 'Resultado de ejercicio guardado'
else:
self.mesagge['text'] = 'No hay rubro'
except (sqlite3.IntegrityError, TclError):
self.mesagge['text'] = 'No hay datos o ya esta cargado'
The problem is that
selection_get
it is not a method ofListBox
, it is common to all the widgets that export the selected item to the X system or to the Windows clipboard. You have no way of knowing which widgets exported that text, in fact it might not be one of yoursListBox
if you interact with another widget that also exports its selection in between.You can get the item from one
ListBox
withlistbox.get(indice)
. To obtain the index you cannot use ittk.ACTIVE
because even if you click on another list, the first one will still have an active one. What you can use islistbox.curselection
. This generates another problem, if all the lists lose focus, nothing will be selected.I usually approach this by creating a custom class that allows grouping several listboxes so that only one item can be selected from all of them, in addition to making it easier to obtain the item itself: