hpareek

GUI-SQLITE Link_1

Jun 9th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1.             import wx
  2.             import wx.grid
  3.             import os
  4.             import sqlite3
  5.  
  6.             cwd = os.path.abspath(os.curdir)
  7.              
  8.             def connect():                                                                  #to connect with database
  9.                  con_str=cwd+ '/Data/RAMAN.db'
  10.                  cnn = sqlite3.connect(con_str)
  11.                  return cnn
  12.                  cnn.close()
  13.                  
  14.             def data_rows_count():                                                          #to count no of rows in database
  15.                  con = connect()
  16.                  cur.execute("SELECT * FROM ELEMENT")
  17.                  rows=cur.fetchall()
  18.                  i=0
  19.                  for r in rows:
  20.                   i+=1
  21.                  return i
  22.  
  23.             def titling(name):                                                                # to display the names and surnames in uppercase for 1st letter
  24.                 return name.title()
  25.  
  26.             def single_quote_remover(text):                                                   # to remove single quotes from entry to prevent SQL crash
  27.                 return text.replace ("'","/")
  28.                
  29.             class Example(wx.Frame):                                                          # this is the parent frame
  30.                        
  31.                 def __init__(self, parent, title):
  32.                 super(Example, self).__init__(parent, title=title, size=(800,600))        
  33.    
  34.                 self.InitUI()
  35.                 self.Layout()
  36.                 self.Centre()
  37.                 self.Show()
  38.                  
  39.                 def refresh_data(self):
  40.                  cnn =connect()
  41.                  cur = cnn.cursor()
  42.                  cur.execute("SELECT * FROM ELEMENT")
  43.                  rows=cur.fetchall()
  44.                  for i in range (0,len(rows)):
  45.                      for j in range(0,4):
  46.                      cell = rows[i]
  47.                      self.grid_1.SetCellValue(i,j,str(cell[j]))  
  48.                      
  49.                 def InitUI(self):
  50.                 p = wx.Panel(self)
  51.                    
  52.                 bs = wx.BoxSizer(wx.VERTICAL)
  53.                 self.t1 = wx.TextCtrl(p,size = (120,30),style = wx.TE_MULTILINE |wx.TE_CENTER
  54.             )
  55.                 bs.Add(self.t1, 1, wx.EXPAND)
  56.                    
  57.                 gs = wx.GridSizer(11, 18, 5, 5)
  58.                 bs.Add(gs, 1, wx.EXPAND)   
  59.    
  60.                 for i in RAMAN.db:
  61.                     btn = wx.Button(p, -1, i, (10,20))
  62.                     btn.myname = i
  63.                     btn.Bind(wx.EVT_BUTTON, self.OnClick, btn)
  64.                     gs.Add(btn, 0, wx.EXPAND)
  65.                    
  66.    
  67.                 p.SetSizer(bs)
  68.              
  69.                 def OnClick(self, event):
  70.                 name = event.GetEventObject().myname
  71.                 self.t1.AppendText(name)
  72.                 self.t1.AppendText("\n")
  73.    
  74.                 cnn.commit()
  75.                 cnn.close()  
  76.    
  77.             app = wx.App()
  78.             Example(None, title = 'Grid demo')
  79.             app.MainLoop()
Add Comment
Please, Sign In to add comment