Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/03/2013, 01:34
hiramhzr
 
Fecha de Ingreso: enero-2011
Ubicación: En un Cuarto Cubierto de Pasto Verde
Mensajes: 95
Antigüedad: 13 años, 3 meses
Puntos: 3
wxpython: Separar lo visual de la lógica de la aplicación

La situación es la siguiente, estoy tratando de dividir mi ejemplo en dos la parte visual en un archivo y digamos la lógica en otro, (adjunto código) por ejemplo no carga los menus que agredo (linea 17 archivo save.py), lo que quiero hacer es que al presionar el botón (linea 57, 58) cargar el método de la clase alMetodos correspondiente a cada botón pero no logro hacerlo, agradezco de ante mano su ayuda, saludos.

Código Python:
Ver original
  1. #inicio.py
  2.  
  3. import wx
  4. import os
  5. import sqlite3 as lite
  6.  
  7. from save import wSave
  8. #from dos import MyPanel
  9.  
  10. class MyFrame(wx.Frame):
  11.     def __init__(self):
  12.         wx.Frame.__init__(self, None, wx.ID_ANY,
  13.                           "Prueba", size=(300, 400))  
  14.         self.InitUI()
  15.         self.Centre()
  16.         self.Show()
  17.  
  18.     def InitUI(self):
  19.         panel = wSave(self, style=wx.TAB_TRAVERSAL)
  20.  
  21. #---------------------------------------------------------------------
  22. # Run the program
  23. if __name__ == "__main__":
  24.     app = wx.App(False)
  25.     frame = MyFrame()
  26.     app.MainLoop()

Código Python:
Ver original
  1. #save.py
  2.  
  3. import wx
  4. import os
  5. import sqlite3 as lite
  6.  
  7. from metodos import alMetodos
  8.  
  9. class wSave(wx.Panel):
  10.     def __init__(self, *args, **kwargs):
  11.         kwargs['style'] = wx.TAB_TRAVERSAL
  12.         wx.Panel.__init__(self, *args, **kwargs)
  13.        
  14.         menubar = wx.MenuBar()
  15.  
  16.         fileMenu = wx.Menu()
  17.         fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
  18.         menubar.Append(fileMenu, '&File')
  19.         self.SetMenuBar(menubar)
  20.         self.Bind(wx.EVT_MENU, alMetodos.OnQuit, fitem)
  21.        
  22.         help = wx.Menu()
  23.         fitemdos = help.Append(-1, '&About')
  24.         menubar.Append(help, '&Help')
  25.         self.SetMenuBar(menubar)
  26.         self.Bind(wx.EVT_MENU, alMetodos.OnAboutBox, fitemdos)              
  27.        
  28.         vbox = wx.BoxSizer(wx.VERTICAL)
  29.        
  30.         hbox = wx.BoxSizer(wx.HORIZONTAL)
  31.         st1 = wx.StaticText(self,-1,label='Nombre: ')  
  32.         self.tc1 = wx.TextCtrl(self, -1, size=(100, -1))
  33.     self.tc1.SetFocus()
  34.  
  35.         hbox.Add(st1)
  36.         hbox.Add(self.tc1, flag = wx.LEFT, border = 10)
  37.         vbox.Add(hbox, flag=wx.LEFT | wx.TOP, border=40)
  38.         vbox.Add((-1, 10))
  39.  
  40.     hbox2 = wx.BoxSizer(wx.HORIZONTAL)
  41.     st2 = wx.StaticText(self,-1, label=u"Anios: ")
  42.         self.tc2 = wx.TextCtrl(self, -1, size=(100,-1))
  43.         hbox2.Add(st2)
  44.         hbox2.Add(self.tc2, flag=wx.LEFT, border=25)
  45.         vbox.Add(hbox2, flag=wx.LEFT | wx.TOP, border=40)
  46.         vbox.Add((-1,10))  
  47.    
  48.     hbox3 = wx.BoxSizer(wx.HORIZONTAL)
  49.     st3 = wx.StaticText(self, -1, label=u"Apodo: ")
  50.     self.tc3 = wx.TextCtrl(self, -1, size=(100, -1))
  51.     hbox3.Add(st3)
  52.     hbox3.Add(self.tc3, flag=wx.LEFT, border=20)
  53.     vbox.Add(hbox3, flag=wx.LEFT | wx.TOP | wx.BOTTOM, border=40)
  54.     vbox.Add((-1,10))
  55.  
  56.     hbox4 = wx.BoxSizer(wx.HORIZONTAL)
  57.     btn = wx.Button(self, label="Guardar")
  58.     cle = wx.Button(self, label="Limpiar")
  59.         btn.Bind(wx.EVT_BUTTON, alMetodos.onSave)
  60.     cle.Bind(wx.EVT_BUTTON, alMetodos.onCle)
  61.     #para espacio entre conponentes es con flag al igual que hbox y esas cosas
  62.         hbox4.Add(btn,flag=wx.LEFT)
  63.     hbox4.Add(cle,flag=wx.LEFT|wx.BOTTOM, border=10)
  64.     vbox.Add(hbox4, flag=wx.LEFT, border=40)
  65.     vbox.Add((-1,15))
  66.  
  67.         self.tc1.MoveAfterInTabOrder(self.tc3)
  68.         self.tc2.MoveAfterInTabOrder(self.tc1)
  69.         self.tc3.MoveAfterInTabOrder(self.tc2)
  70.  
  71.     self.SetSizer(vbox)
  72.        
  73. #----------------------------------------------------------------------
  74. # Run the program
  75. if __name__ == "__main__":
  76.     app = wx.App(False)
  77.     frame = monti()
  78.     app.MainLoop()

Código Python:
Ver original
  1. #metodos.py
  2. import wx
  3. import os
  4. import sqlite3 as lite
  5.  
  6. class alMetodos:
  7.  
  8.     def OnQuit(self, e):
  9.         dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
  10.         wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  11.         ret = dial.ShowModal()
  12.         if ret == wx.ID_YES:
  13.             self.Destroy()
  14.         else:
  15.             self.Show()
  16.  
  17. #----------------------------------------------------------------------
  18. #About de la aplicacion        
  19.     def OnAboutBox(self, e):
  20.        
  21.         description = """  .
  22. """
  23.         licence = """Puede ser usado por cualquiera."""
  24.  
  25.         info = wx.AboutDialogInfo()
  26.         info.SetIcon(wx.Icon('hunter.png', wx.BITMAP_TYPE_PNG))
  27.         info.SetName('Monti')
  28.         info.SetVersion('0.1a')
  29.         info.SetDescription(description)
  30.         info.SetCopyright('(C) 2013 Hiram')
  31.         info.SetWebSite('http://valles.servehttp.com')
  32.         info.SetLicence(licence)
  33.         info.AddDeveloper('Hiram Zuniga')
  34.         info.AddDocWriter('Hiram Zuniga')
  35.         info.AddArtist('Hiram')
  36.         info.AddTranslator('YORCH')
  37.  
  38.         wx.AboutBox(info)
  39.  
  40. #----------------------------------------------------------------------
  41. # Metoth questyon before to close      
  42.     def OnClose(self, event):
  43.         dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
  44.         wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  45.         ret = dial.ShowModal()
  46.         if ret == wx.ID_YES:
  47.             self.Destroy()
  48.         else:
  49.             self.Show()
  50.  
  51. #-----------------------------------------------------------------------
  52. #Guarda registro y limpia por si agregan uno nuevo
  53.     def onSave(self, event):
  54.         try:
  55.             con = lite.connect('people')
  56.             cur = con.cursor()
  57.             name = self.tc1.GetValue()
  58.             age = self.tc2.GetValue()
  59.             remark = self.tc3.GetValue()
  60.             cur.execute('insert into neighbours values(?, ?, ?)',
  61.             (name, age, remark))
  62.             con.commit()
  63.             cur.close()
  64.             con.close()
  65.             self.tc1.Clear()
  66.             self.tc2.Clear()
  67.             self.tc3.Clear()
  68.         except lite.Error, error:
  69.             dlg = wx.MessageDialog(self, str(error), 'Error occured')
  70.             dlg.ShowModal()
  71.  
  72.     def onCle(self,event):
  73.     self.tc1.Clear()
  74.     self.tc2.Clear()
  75.     self.tc3.Clear()