wxPython: Rubrica- VIEWs parte 2
GUI aggiunta contatto
…riprendiamo dal frame per l’aggiunta di un contatto.
Lo stesso frame lo utilizzerò, con qualche differenza, anche per la modifica dei contatti esistenti.
Non ho utilizzato sizers per pigrizia, ma il risultato è comunque buono.
codice:
#!/usr/bin/python #ContactFrame.py '''Contact Frame for Rubrica App This frame is used as Add Contact Frame, and Edit Contact Frame ''' import wx from wx.lib.buttons import GenBitmapTextButton class FrameContact(wx.Frame): '''Initial Core Frame''' def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) self.panel = wx.Panel(self, wx.ID_ANY) self.SetBackgroundColour('#00CCCC') font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD) self.title = wx.StaticText(self.panel, wx.ID_ANY, 'Edit Contact', (15, 10)) self.title.SetFont(font) wx.StaticLine(self.panel, wx.ID_ANY, (0, 35), (325, 3)) wx.StaticLine(self.panel, wx.ID_ANY, (0, 185), (325, 3)) self.s_sur = wx.StaticText(self.panel, wx.ID_ANY, 'Surname', (5, 55), style = wx.ALIGN_LEFT) self.s_nam = wx.StaticText(self.panel, wx.ID_ANY, 'Name', (5, 75), style = wx.ALIGN_LEFT) wx.StaticText(self.panel, wx.ID_ANY, 'Address', (5, 95), style = wx.ALIGN_LEFT) wx.StaticText(self.panel, wx.ID_ANY, 'Mail', (5, 115), style = wx.ALIGN_LEFT) wx.StaticText(self.panel, wx.ID_ANY, 'Phone', (5, 135), style = wx.ALIGN_LEFT) self.s_mob = wx.StaticText(self.panel, wx.ID_ANY, 'Mobile', (5, 155), style = wx.ALIGN_LEFT) self.t_surname = wx.TextCtrl(self.panel, wx.ID_ANY, pos = (70, 50), size = (250, 20), style = wx.ALIGN_LEFT) self.t_name = wx.TextCtrl(self.panel, wx.ID_ANY, pos = (70, 70), size = (250, 20), style = wx.ALIGN_LEFT) self.t_address = wx.TextCtrl(self.panel, wx.ID_ANY, pos = (70, 90), size = (250, 20), style = wx.ALIGN_LEFT) self.t_mail = wx.TextCtrl(self.panel, wx.ID_ANY, pos = (70, 110), size = (250, 20), style = wx.ALIGN_LEFT) self.t_phone = wx.TextCtrl(self.panel, wx.ID_ANY, pos = (70, 130), size = (250, 20), style = wx.ALIGN_LEFT) self.t_mobile = wx.TextCtrl(self.panel, wx.ID_ANY, pos = (70, 150), size = (250, 20), style = wx.ALIGN_LEFT) self.btn_ed_save = GenBitmapTextButton(self.panel, wx.ID_ANY, wx.Bitmap('save.png'), 'Save'.rjust(20), (5, 200), (150, -1)) self.btn_ed_save.SetBezelWidth(1) self.btn_ed_save.SetBackgroundColour('#c2e6f8') self.btn_ed_exit = GenBitmapTextButton(self.panel, wx.ID_ANY, wx.Bitmap('cancel.png'), 'Cancel'.rjust(20), (160, 200), (160, -1)) self.btn_ed_exit.SetBezelWidth(1) self.btn_ed_exit.SetBackgroundColour('#c2e6f8') self.btn_ed_save.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_ed_save.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.btn_ed_exit.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_ed_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): '''Add Contact frame starter''' app = wx.PySimpleApp() edit = FrameContact(parent, -1, title = 'Save Contact', size = (330, 300)) edit.title.SetLabel('Save new Contact') edit.s_sur.SetLabel('Surname*') edit.s_nam.SetLabel('Name*') edit.s_mob.SetLabel('Mobile*') app.MainLoop() if __name__ == '__main__': main_core(parent = None)
l’esito è:
Nel codice ho inserito anche 2 bind, per cambiare il colore del pulsante quando ci si passa con il puntatore del mouse, e l’altro per rimettere il background iniziale.
Per correttezza avrei dovuto creare i due bind esterni alla View (nel Controller), come tutti gli altri, ma non volendo ingigantire troppo il modulo Controller, ho gestito i due bottoni con eventi e callbacks, all’interno del frame.
GUI gestione contatti
Con questo frame, avrò la vera e propria agenta, con un bottone per ogni lettera, una listctrl per la visualizzazione dei contatti e la possibilità di cancellarli o modificarli.
il codice:
#!/usr/bin/python # Find.py '''Find Contact Frame for Rubrica App Double click on contact, enables the delete button and you can delete the selected contact. Right click on contact, enables the edit frame that modifies the contact values and stores the data to database ''' import wx from wx.lib.buttons import GenBitmapTextButton class FrameFindContact(wx.Frame): '''Contact Manager Frame''' def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) self.panel = wx.Panel(self, -1) self.SetBackgroundColour('#00CCCC') letters = [chr(i).upper() for i in xrange(ord('a'), ord('z')+1)] self.pos = 5 for letter in letters: button = wx.Button(self.panel, wx.ID_ANY, letter, (self.pos, 3), (26, 30)) button.SetBackgroundColour('#c2e6f8') self.pos += 26 wx.StaticLine(self.panel, wx.ID_ANY, (0, 35), (700, 3)) self.list_ctrl = wx.ListCtrl(self.panel, wx.ID_ANY, (0, 50), (690, 200), wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES) self.list_ctrl.InsertColumn(0, "Surname", wx.LIST_FORMAT_LEFT, 100) self.list_ctrl.InsertColumn(1, "Name", wx.LIST_FORMAT_LEFT, 100) self.list_ctrl.InsertColumn(2, "Address", wx.LIST_FORMAT_LEFT, 150) self.list_ctrl.InsertColumn(3, "mail", wx.LIST_FORMAT_LEFT, 150) self.list_ctrl.InsertColumn(4, "phone", wx.LIST_FORMAT_LEFT, 100) self.list_ctrl.InsertColumn(5, "mobile", wx.LIST_FORMAT_LEFT, 100) self.list_ctrl.SetFont(wx.Font(8, wx.NORMAL, wx.NORMAL, wx.NORMAL)) wx.StaticLine(self.panel, wx.ID_ANY, (0, 270), (700, 3)) self.btn_trash = GenBitmapTextButton(self.panel, wx.ID_ANY, wx.Bitmap('trash.png'), 'Delete'.rjust(20), (10, 300), (340, -1)) self.btn_trash.SetBezelWidth(1) self.btn_trash.SetBackgroundColour('#c2e6f8') self.btn_find_exit = GenBitmapTextButton(self.panel, wx.ID_ANY, wx.Bitmap('cancel.png'), 'Cancel'.rjust(20), (345, 300), (340, -1)) self.btn_find_exit .SetBezelWidth(1) self.btn_find_exit .SetBackgroundColour('#c2e6f8') self.Centre() self.Show() def main_core(parent): '''Add Contact frame starter''' app = wx.PySimpleApp() FrameFindContact(parent, -1, title = 'Find Contact', size = (700, 400)) app.MainLoop() if __name__ == '__main__': main_core(parent = None)
GUI Cancellazione contatto
Come ultimo frame, per una cancellazione più agile di un contatto,
#!/usr/bin/python #DelContactFrame.py '''Delete Contact Frame for Rubrica App''' import wx from wx.lib.buttons import GenBitmapTextButton class FrameDelContact(wx.Frame): '''Initial Core Frame''' def __init__(self, *args, **kwargs): contacts = kwargs.pop('contacts') wx.Frame.__init__(self, *args, **kwargs) panel = wx.Panel(self, wx.ID_ANY) self.SetBackgroundColour('#00CCCC') font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD) title = wx.StaticText(panel, wx.ID_ANY, 'Choose Contact to Delete', (70, 10)) title.SetFont(font) self.choice = wx.Choice(panel, wx.ID_ANY, (60, 30), size = (200, 30), choices = contacts) wx.StaticLine(panel, wx.ID_ANY, (0, 70), (325, 3)) text = wx.StaticText(panel, wx.ID_ANY, 'Contact to Delete: ', (10, 80), style = wx.ALIGN_CENTER) text.SetFont(font) self.name_to_del = wx.StaticText(panel, wx.ID_ANY, '', (140, 82), style = wx.ALIGN_CENTER) wx.StaticLine(panel, wx.ID_ANY, (0, 100), (325, 3)) self.btn_del = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('trash.png'), 'Delete'.rjust(20), (5, 110), (150, -1)) self.btn_del.SetBezelWidth(1) self.btn_del.SetBackgroundColour('#c2e6f8') self.btn_exit_del = GenBitmapTextButton(panel, wx.ID_ANY, wx.Bitmap('cancel.png'), 'Cancel'.rjust(20), (160, 110), (150, -1)) self.btn_exit_del.SetBezelWidth(1) self.btn_exit_del.SetBackgroundColour('#c2e6f8') self.choice.Bind(wx.EVT_CHOICE, self.on_choice) 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_exit_del.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter) self.btn_exit_del.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave) self.Centre() self.Show() def on_choice(self, evt): '''Choice event handler''' self.name_to_del.SetLabel(self.choice.GetStringSelection()) 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): '''Add Contact frame starter''' app = wx.PySimpleApp() list_c = ['Banchelli', 'Bologna', 'Martelli'] FrameDelContact(parent, -1, title = 'Delete Contact', size = (325, 200), contacts = list_c) app.MainLoop() if __name__ == '__main__': main_core(parent = None)
ora passiamo al modulo Model.
continua…
Commenti recenti