Hi, I'm new to Python, I'm practicing queries sqlite3 database, I will present what I have so far.
until now I have not seen an example of how to reflect queries a database using wxPython
I hope I help, do not write very good English.
frame.py
import wx
import sqlite3 as dbapi
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
MyPanel(self)
class MyPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
inventario = wx.StaticText(self, -1, u'Bienvenido Inventario: ', pos = (35,10))
codigo = wx.StaticText(self, -1, 'Codígo: ', pos = (20,30))
self.codigo_cuadro = wx.TextCtrl(self, -1, '', pos = (20,50))
producto = wx.StaticText(self, -1, 'Producto: ', pos = (20,80))
self.producto_cuadro = wx.TextCtrl(self, -1, '', pos = (20,100))
costo = wx.StaticText(self, -1, 'Costo: ', pos = (20,130))
self.costo_cuadro = wx.TextCtrl(self, -1, '', pos = (20,150))
guardar = wx.Button(self, -1, 'Guardar', pos = (20,180))
buscar = wx.Button(self, -1, 'Buscar', pos = (20,210))
salir = wx.Button(self, -1, 'Salir', pos = (20,240))
guardar.Bind(wx.EVT_BUTTON, self.OnGuardar)
buscar.Bind(wx.EVT_BUTTON, self.OnBuscar)
salir.Bind(wx.EVT_BUTTON, self.OnSalir)
def OnGuardar(self, evt):
bd = dbapi.connect("formulario.dat")
cursor = bd.cursor()
cursor.execute("""create table if not exists inventario (codigo_cuadro txt, producto_cuadro txt, costo_cuadro txt)""")
bd.commit()
cursor.close()
bd.close()
ingresodecodigo = self.codigo_cuadro.GetValue()
dialogo = wx.MessageDialog(self, 'El producto %s, se ha guardado correctamente' % (ingresodecodigo), 'Información', wx.OK | wx.ICON_INFORMATION)
dialogo.ShowModal()
self.codigo_cuadro.Clear()
self.producto_cuadro.Clear()
self.costo_cuadro.Clear()
dialogo.Destroy()
def OnBuscar(self, evt):
from prueba import MyFrame2
def OnSalir(self, evt):
self.Parent.Close()
class App(wx.App):
def OnInit(self):
f = MyFrame(parent = None, title = u'Inventario', size = (200,400), pos = (320,150))
f.Show()
return True
aplicacion = App(0)
aplicacion.MainLoop()
call to prueba.py
import wx
import sqlite3
conn = sqlite3.connect('formulario.dat')
cur = conn.cursor()
cur.execute("""SELECT * FROM inventario""")
list = list(cur.fetchall())
index = range(len(list))
class App(wx.App):
def OnInit(self):
self.ventana = wx.Frame(parent = None, title = u'Resultados.', size = (200,400), pos = (320,150))
panel = wx.Panel(self.ventana, -1)
codigo = wx.StaticText(panel, -1, 'Codigo', pos = (20,30))
self.codigocuadro = wx.TextCtrl(panel, -1, '', pos = (20, 50))
self.codigocuadro.Bind(wx.EVT_TEXT, self.Buscar)
return True
def Buscar(self, evt):
cur.execute("""SELECT * FROM inventario""")
all = cur.fetchall()
criterio = self.codigocuadro.GetValue()
if criterio <> '':
cur.execute("""SELECT * FROM inventario WHERE codigo_cuadro LIKE ('%%%s%%')""" %(criterio))
items = cur.fetchall()
else:
items = []
aplicacion = App()
aplicacion.MainLoop()
Thanks .. I hope I help please