Advertisement
Guest User

Untitled

a guest
Apr 16th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.09 KB | None | 0 0
  1. import wx, sqlite3, MainFrame
  2. from functools import partial
  3. border = 10
  4.  
  5. #intial databse operations
  6. conn = sqlite3.connect('data.sqlite')
  7. cur = conn.cursor()
  8.        
  9. cur.execute("""SELECT * FROM people""")
  10. list = list(cur.fetchall())
  11. index = range(len(list))
  12.  
  13. class PickupFrame(wx.Frame):
  14.    
  15.     def __init__(self, id):
  16.        
  17.         wx.Frame.__init__(self, parent = None, id = wx.ID_ANY, size = (600, 500), title = 'Pickup')
  18.         panel = wx.Panel(self)
  19.         self.Centre()
  20.        
  21.         if id == 1:
  22.             self.Maximize()
  23.         self.id = id
  24.        
  25.         self.search_array = []
  26.        
  27. #---------------------------------------------------------------------------------------------------       
  28.        
  29.         #create input panel
  30.         input_panel = wx.Panel(panel, style = wx.SIMPLE_BORDER)
  31.         name_msg = wx.StaticText(input_panel, label = 'Name:')
  32.         self.name_box = wx.TextCtrl(input_panel, wx.ID_ANY, '')
  33.         phone_msg = wx.StaticText(input_panel, label = 'Phone Number:')
  34.         self.phone_box = wx.TextCtrl(input_panel, wx.ID_ANY, '')
  35.        
  36.         self.name_box.Bind(wx.EVT_TEXT, partial(self.Search, id = 0))
  37.         self.phone_box.Bind(wx.EVT_TEXT, partial(self.Search, id = 1))
  38.        
  39.         #create operation buttons
  40.         save_button = wx.Button(input_panel, label = 'Save')
  41.         ok_button = wx.Button(input_panel, label = 'OK')
  42.         delete_button = wx.Button(input_panel, label = 'Delete')
  43.         cancel_button = wx.Button(input_panel, label = 'Cancel')
  44.         menu_button = wx.Button(input_panel, label = 'Main Menu')
  45.        
  46.         #bind buttons to event handlers
  47.         save_button.Bind(wx.EVT_BUTTON, self.SaveContacts)
  48.         delete_button.Bind(wx.EVT_BUTTON, self.DeleteContact)
  49.         cancel_button.Bind(wx.EVT_BUTTON, self.CloseFrame)
  50.         menu_button.Bind(wx.EVT_BUTTON, self.Menu)
  51.        
  52.         #layout buttons
  53.         btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
  54.         btn_sizer.Add(save_button, 1, wx.EXPAND | wx.LEFT, border)
  55.         btn_sizer.Add(delete_button, 1, wx.EXPAND, border)
  56.         btn_sizer.Add(ok_button, 1, wx.EXPAND, border)
  57.         btn_sizer.Add(cancel_button, 1, wx.EXPAND, border)
  58.        
  59.         #layout messages and input boxes
  60.         msg_box_sizer = wx.BoxSizer(wx.VERTICAL)
  61.         msg_box_sizer.Add(name_msg, 0, wx.LEFT, border)
  62.         msg_box_sizer.Add(self.name_box, 0, wx.LEFT | wx.BOTTOM, border)
  63.         msg_box_sizer.Add(phone_msg, 0, wx.LEFT, border)
  64.         msg_box_sizer.Add(self.phone_box, 0, wx.LEFT | wx.BOTTOM, border)
  65.         msg_box_sizer.Add(btn_sizer)
  66.         msg_box_sizer.Add(menu_button, 0, wx.ALL, border)
  67.        
  68.         input_panel.SetSizer(msg_box_sizer)
  69.        
  70. #-------------------------------------------------------------------------
  71.        
  72.         #create display panel
  73.         display_panel = wx.Panel(panel, style = wx.SUNKEN_BORDER)
  74.        
  75.         #create table
  76.         self.grid = wx.ListCtrl(display_panel, style = wx.LC_REPORT)
  77.         self.grid.InsertColumn(0, 'Name')
  78.         self.grid.InsertColumn(1, 'Phone')
  79.        
  80.         #insert existing values
  81.         for count in index:
  82.             self.grid.InsertStringItem(count, list[count][0])
  83.             self.grid.SetStringItem(count, 1, list[count][1])
  84.            
  85.         #layout display panel
  86.         d_panel_sizer = wx.BoxSizer(wx.VERTICAL)
  87.         d_panel_sizer.Add(self.grid, 1, wx.EXPAND, border)
  88.         display_panel.SetSizer(d_panel_sizer)
  89.            
  90.         #layout whole panel
  91.         panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
  92.         panel_sizer.Add(input_panel, 1, wx.EXPAND | wx.ALL, border)
  93.         panel_sizer.Add(display_panel, 1, wx.EXPAND | wx.ALL, border)
  94.         panel.SetSizer(panel_sizer)
  95.        
  96.     def SaveContacts(self, event):
  97.        
  98.         if self.name_box.GetValue() == '' or self.phone_box.GetValue() == '':
  99.            
  100.             error_frame = wx.Frame(None, wx.ID_ANY, 'Error')
  101.             error_panel = wx.Panel(error_frame)
  102.             error_text = wx.StaticText(error_panel, wx.ID_ANY, 'Neither field can be blank. Please check again.')
  103.            
  104.             txt_sizer = wx.BoxSizer(wx.VERTICAL)
  105.             txt_sizer2 = wx.BoxSizer(wx.HORIZONTAL)
  106.             txt_sizer.Add(error_text, 0, wx.ALIGN_CENTRE | wx.EXPAND)
  107.             txt_sizer2.Add(txt_sizer, 0, wx.ALIGN_CENTRE | wx.EXPAND)
  108.             error_panel.SetSizer(txt_sizer2)
  109.             error_frame.Show()
  110.            
  111.         else:
  112.        
  113.             #assigns user input to variables
  114.             name = self.name_box.GetValue()
  115.             phone = self.phone_box.GetValue()
  116.        
  117.             cur.execute("""INSERT INTO people (name, phone) VALUES (?, ?)""", (name, phone))
  118.                            
  119.             conn.commit()
  120.        
  121.             #clears input boxes
  122.             self.name_box.Clear()
  123.             self.phone_box.Clear()
  124.        
  125.             #refreshes grid with new contact
  126.             self.grid.DeleteAllItems()
  127.             cur.execute("""SELECT * FROM people""")
  128.             items = cur.fetchall()
  129.             for i in range(len(items)):
  130.                 self.grid.InsertStringItem(i, items[i][0])
  131.                 self.grid.SetStringItem(i, 1, items[i][1])
  132.    
  133.     def DeleteContact(self, event):
  134.  
  135.         #delete row from grid
  136.         item_index = self.grid.GetFocusedItem()
  137.         self.grid.DeleteItem(item_index)
  138.        
  139.         #creates array to correspond grid index and database index (row_id)
  140.         cur.execute("""SELECT rowid FROM people""")
  141.         ids = cur.fetchall()
  142.         no = range(len(ids))
  143.  
  144.         array = []
  145.        
  146.         for count in no:
  147.             array.append(ids[count][0])
  148.        
  149.         cur.execute("""DELETE FROM people WHERE rowid = ('%i')""" % (array[item_index]))   
  150.        
  151.         conn.commit()
  152.        
  153.     def Search(self, event, id):
  154.        
  155.         self.grid.DeleteAllItems()
  156.        
  157.         cur.execute("""SELECT * FROM people""")
  158.         all = cur.fetchall()
  159.        
  160.         if id == 0:
  161.             criteria = self.name_box.GetValue()
  162.             if criteria <> '':
  163.                 cur.execute("""SELECT * FROM people WHERE name LIKE ('%%%s%%')""" % (criteria))
  164.                 items = cur.fetchall()
  165.             else:
  166.                 items = []
  167.         else:
  168.             criteria = self.phone_box.GetValue()
  169.             if criteria <> '':
  170.                 cur.execute("""SELECT * FROM people WHERE phone LIKE ('%%%s%%')""" % (criteria))
  171.                 items = cur.fetchall()
  172.             else:
  173.                 items = []
  174.        
  175.         if items == []:
  176.             for i in range(len(all)):
  177.                 self.grid.InsertStringItem(i, all[i][0])
  178.                 self.grid.SetStringItem(i, 1, all[i][1])
  179.         else:
  180.             for i in range(len(items)):
  181.                 self.grid.InsertStringItem(i, items[i][0])
  182.                 self.grid.SetStringItem(i, 1, items[i][1])
  183.                
  184.     def Menu(self, event):
  185.    
  186.         cur.close()
  187.         self.Destroy()
  188.         menu = MainFrame.MainFrame()
  189.         menu.Show()
  190.         menu.Centre()
  191.    
  192.     def OK(self, event):
  193.    
  194.         global n, p
  195.         n = self.name_box.GetValue()
  196.         p = self.phone_box.GetValue()
  197.    
  198.     def CloseFrame(self, event):
  199.        
  200.         cur.close()
  201.         self.Destroy()
  202.        
  203. class Result():
  204.  
  205.     def Result(self):
  206.    
  207.         return n,p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement