Código Python:
Ver original#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
import wx
class User(object):
def __init__(self, _id, name, active):
self.id = _id
self.name = name
self.active = active
def is_active(self):
return self.active == 'S'
def set_active(self, active):
if active:
self.active = 'S'
else:
self.active = 'N'
def __str__(self):
return self.name
class Users(object):
def __init__(self):
self.user_list = []
def add(self, user):
self.user_list.append(user)
def __iter__(self):
return iter(self.user_list)
def get_users():
db_connection = lite.connect('db.usuarios')
users = Users()
with db_connection:
cursor = db_connection.cursor()
cursor.execute("SELECT id, nombre, activo FROM Usuarios")
for _id, name, active in cursor:
user = User(_id, name, active)
users.add(user)
return users
def save_users(users):
db_connection = lite.connect('db.usuarios')
with db_connection:
cursor = db_connection.cursor()
for user in users:
query = "UPDATE Usuarios SET activo=?, nombre=? WHERE id=?"
params = (user.active, user.name, user.id)
cursor.execute(query, params)
db_connection.commit()
class RadioButtonFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'User Admin', size=(200, 200))
self.Bind(wx.EVT_CLOSE, self.OnClose)
panel = wx.Panel(self, -1)
self.widgets = []
self.users = get_users()
for i, user in enumerate(self.users):
radio = wx.RadioButton(panel, -1, label=str(i+1), pos=(20, 50+(i*30)))
text = wx.TextCtrl(panel, -1, user.name, pos=(80, 50+(i*30)))
text.Enable(False)
if user.is_active():
radio.SetValue(True)
text.Enable(True)
def OnButton(event, radio_index=i):
self.OnRadio(event, radio_index)
radio.Bind(wx.EVT_RADIOBUTTON, OnButton)
widget = {'radio': radio, 'text': text}
self.widgets.append(widget)
def OnRadio(self, event, index):
for widget in self.widgets:
widget['text'].Enable(False)
widget = self.widgets[index]
widget['text'].Enable(True)
def OnClose(self, event):
dlg = wx.MessageDialog(self, "Guardar los cambios realizados?", "Confirmar Salida", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
for user, widget in zip(self.users, self.widgets):
user.set_active(widget['radio'].GetValue())
user.name = widget['text'].GetValue()
save_users(self.users)
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frm = RadioButtonFrame()
frm.Show()
app.MainLoop()
Mi intento de hacer esta pequeña app. No esta bien estructurada y hay SQL generado por un humano de por medio, pero hay te dejo la prueba.