Hi all,
I am working on program that will connect to MySQL database, colect data from table and show them in grid. Where user can edit cell, and then on enter key changes will bi save directly into database table. Curent problem is that I am stuck at importing data into grid. I know that need for loop and SetCellValue, but not sure how to configure that. Can anyone help me.
Here is code:
import wx
import wx.grid as gridlib
import MySQLdb
#Class main grid
class MyGrid(gridlib.Grid):
def __init__(self, parent):
gridlib.Grid.__init__(self, parent, )
# Connecting to database
def getData():
db = MySQLdb.connect("127.0.0.1", "user", "pass", "database")
cursor = db.cursor()
query = "SELECT * FROM setup"
cursor.execute(query)
data = []
res = cursor.fetchall()
for i in res:
data.append(i)
return data
#Column name
colnames = ["idSetup", "idKomitent", "Drzava", "Prg", "Provajder", "ServerName", "DataBaseName", "Sifarnici",
"Promena"]
#Setup
data = getData()
self.data = data
self.colnames = colnames
#Number of rows
def GetNumberRows(self):
return int(len(self.data))
#Number of columns
def GetNumberCols(self):
return int(len(self.colnames))
#Grid creation
col = GetNumberCols(self)
row = GetNumberRows(self)
self.CreateGrid(row, col)
self.ChangedValue = False
#Seting up name of columns
self.SetColLabelValue(0, "idSetup")
self.SetColLabelValue(1, "idKomitent")
self.SetColLabelValue(2, "Drzava")
self.SetColLabelValue(3, "Prg")
self.SetColLabelValue(4, "Provajder")
self.SetColLabelValue(5, "ServerName")
self.SetColLabelValue(6, "DataBaseName")
self.SetColLabelValue(7, "Sifarnici")
self.SetColLabelValue(8, "Promena")
#Insertind data
def GetValue(self, row, col):
return str(self.data[row][1].get(self.GetColLabelValue(col), ""))
def SetValue(self, row, col, value):
self.data[row][1][self.GetColLabelValue(col)] = value
#Here is where I am stuck
for i in data:
for k in i:
self.SetCellValue(row, col, '%s')
#Events
self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.onCellChange)
self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
def onCellChange(self, evt):
pass
#Enter key down
def onKeyDown(self, evt):
if evt.GetKeyCode() != wx.WXK_RETURN:
evt.Skip()
return
if evt.ControlDown(): # the edit control needs this key
evt.Skip()
return
self.DisableCellEditControl()
success = self.MoveCursorRight(evt.ShiftDown())
if not success:
newRow = self.GetGridCursorRow() + 1
if newRow < self.GetTable().GetNumberRows():
self.SetGridCursor(newRow, 0)
self.MakeCellVisible(newRow, 0)
else:
newRow = self.GetGridCursorRow() + 1
self.appendRow()
self.SetGridCursor(newRow, 0)
self.MakeCellVisible(newRow, 0)
self.ChangedValue = False
def appendRow(self):
pass
#Class main window
class GlavniProzor(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title = "Editor", size = (500, 1000))
self.Show()
self.CenterOnScreen()
#Panel
panel = wx.Panel(self)
mygrid = MyGrid(panel)
#Box
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(mygrid, 1, wx.EXPAND)
panel.SetSizer(sizer)
sizer.Fit(self)
#Run
if __name__ == "__main__":
app = wx.App()
frame = GlavniProzor()
app.MainLoop()