wxPython: Rubrica- VIEWs parte 1
Ho utilizzato (per lo meno ho tentato di farlo) un simil pattern MVC ( senza Observer), comincerò con la parte grafica.
Ho buttato giù:
un’interfaccia core e le sottointerfacce che utilizzerò per l’aggiunta di un nuovo contatto, per la cancellazione di un contatto, per la gestione di un contatto (modifica) e l’ultima interfaccia per un semplice frame di informazione (il classico about).
GUI Core
partiamo con l’interfaccia core, dove ho utilizzato dei GenBitmapTextButton per ottenere dei pulsanti con immagine e testo:
codice:
#!/usr/bin/python #CoreFrame.py '''Core Frame for Rubrica App''' import wx from wx.lib.buttons import GenBitmapTextButton class FrameCore(wx.Frame): '''Initial Core Frame''' def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) panel = wx.Panel(self, wx.ID_ANY) self.SetBackgroundColour('#00CCCC') self.btn_info = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('info.png'), 'About...'.rjust(15), (25, 0), (150, -1)) self.btn_info.SetBezelWidth(1) self.btn_info.SetBackgroundColour('#c2e6f8') self.btn_add = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('add.png'), 'Add Contact'.rjust(15), (25, 48), (150, -1)) self.btn_add.SetBezelWidth(1) self.btn_add.SetBackgroundColour('#c2e6f8') self.btn_del = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('delete.png'), 'Del Contact'.rjust(15), (25, 96), (150, -1)) self.btn_del.SetBezelWidth(1) self.btn_del.SetBackgroundColour('#c2e6f8') self.btn_find = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('search.png'), 'Find Contact'.rjust(15), (25, 144), (150, -1)) self.btn_find.SetBezelWidth(1) self.btn_find.SetBackgroundColour('#c2e6f8') self.btn_exit = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('exit.png'), 'Exit'.rjust(15), (25, 192), (150, -1)) self.btn_exit.SetBezelWidth(1) self.btn_exit.SetBackgroundColour('#c2e6f8') self.btn_find.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_find.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.btn_add.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_add.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.btn_del.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_del.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.btn_info.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_info.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.btn_exit.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_exit.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.Centre() self.Show() def on_btn_enter(self, event): '''Into Button enter-mouse event handler''' obj = event.GetEventObject() obj.SetBackgroundColour('#ffdf85') obj.Refresh() def on_btn_leave(self, event): '''From Button leave-mouse event handler''' obj = event.GetEventObject() obj.SetBackgroundColour('#c2e6f8') obj.Refresh() def main_core(parent): '''Info frame starter''' app = wx.PySimpleApp() FrameCore(parent, -1, title = 'Rubrica', size = (200, 280)) app.MainLoop() if __name__ == '__main__': main_core(parent = None)
l’aspetto è il seguente:
Non dobbiamo preoccuparci di cosa visualizzerà il frame (per quello lavoreranno il controller insieme al Model), ma di come lo visualizzerà.
GUI info:
come in ogni applicazione che si rispetti, mettiamo un bel messaggio di “About”.
Ho utlizzato un HtmlWindow, per ppoter utilizzare il codice html:
#!/usr/bin/python #InfoHTMLFrame '''Info Frame for Core-About choice''' import wx import wx.html from wx.lib.buttons import GenBitmapTextButton class FrameInfo(wx.Frame): '''Frame for Info text''' def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) self.text = ''' <html> <body bgcolor="#99FFCC"> <center><table bgcolor="#00CCCC" width="100%" cellspacing="0" cellpadding="0" border="1"> <tr> <td align="center"><h1>Rubrica v2.0</h1></td> </tr> </table> </center> <p><b>Rubrica</b> is a simple phone-book realized with:<br> <b>- wxPython</b> for Graphics<br> <b>- Sqlite</b> for database structure<br> <b>- SQLAlchemy</b> for Object Ralation Mapping<br> I've tried to use a Model-View-COntroller pattern-like.<br> web-site: <b>www.bancaldo.wordpress.com</b><br> last revision: may 17, 2011</p> </body> </html> ''' self.SetBackgroundColour('#00CCCC') html = wx.html.HtmlWindow(self) html.SetPage(self.text) self.btn_quit = GenBitmapTextButton(self, wx.ID_ANY, wx.Bitmap('quit.png'), ' quit', (25, 150), (150, -1)) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(html, 1, wx.EXPAND|wx.ALL, 5) sizer.Add(self.btn_quit, 0, wx.ALIGN_CENTER|wx.ALL, 5) self.SetSizer(sizer) self.Centre() self.Show() def main_info(parent): '''Info frame starter''' app = wx.PySimpleApp() FrameInfo(parent, -1, title = 'Info', size = (400, 350)) app.MainLoop() if __name__ == '__main__': main_info(parent = None)
Commenti recenti