hpareek

GUI-SQLITE Link

Jun 8th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 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():                              
  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():  
  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.     class Example(wx.Frame):    
  24.                    
  25.         def __init__(self, parent, title):
  26.         super(Example, self).__init__(parent, title=title, size=(800,600))
  27.        
  28.         self.InitUI()
  29.         self.Layout()
  30.         self.Centre()
  31.         self.Show()
  32.          
  33.         def refresh_data(self):
  34.          cnn =connect()
  35.          cur = cnn.cursor()
  36.          cur.execute("SELECT * FROM ELEMENT")
  37.          rows=cur.fetchall()
  38.          for i in range (0,len(rows)):
  39.              for j in range(0,4):
  40.                  cell = rows[i]
  41.                  self.grid_1.SetCellValue(i,j,str(cell[j]))  
  42.         def InitUI(self):
  43.         p = wx.Panel(self)
  44.            
  45.         bs = wx.BoxSizer(wx.VERTICAL)
  46.         self.t1 = wx.TextCtrl(p,size = (120,30),style = wx.TE_MULTILINE |wx.TE_CENTER
  47.     )
  48.         bs.Add(self.t1, 1, wx.EXPAND)
  49.            
  50.         gs = wx.GridSizer(11, 18, 5, 5)
  51.         bs.Add(gs, 1, wx.EXPAND)
  52.        
  53.         def __set_properties(self):  
  54.         r=data_rows_count()
  55.         self.grid_1.CreateGrid(r, 4)
  56.         self.grid_1.SetColLabelValue(0, _("ATOMIC NUMBER"))
  57.         self.grid_1.SetColSize(0, 12)
  58.         self.grid_1.SetColLabelValue(1, _("SYMBOL"))
  59.         self.grid_1.SetColSize(1, 150)
  60.         self.grid_1.SetColLabelValue(2, _("ROW"))
  61.         self.grid_1.SetColSize(2, 150)
  62.         self.grid_1.SetColLabelValue(3, _("COLUMN"))
  63.         self.grid_1.SetColSize(3, 150)
  64.         self.refresh_data()
  65.        
  66.         for i in ELEMENT:
  67.             btn = wx.Button(p, -1, i, (10,20))
  68.             btn.myname = i
  69.             btn.Bind(wx.EVT_BUTTON, self.OnClick, btn)
  70.             gs.Add(btn, 0, wx.EXPAND)
  71.            
  72.        
  73.         p.SetSizer(bs)
  74.      
  75.         def OnClick(self, event):
  76.         name = event.GetEventObject().myname
  77.         self.t1.AppendText(name)
  78.         self.t1.AppendText("\n")
  79.        
  80.         cnn.commit()
  81.         cnn.close()  
  82.        
  83.     app = wx.App()
  84.     Example(None, title = 'Grid demo')
  85.     app.MainLoop()
Add Comment
Please, Sign In to add comment