Dear wxpython experts,
I would like to know how to bind a grid selection with the text of
wx.TextCtrl. This is the code I put together from the different
examples given on the tutorials:
import wx
import csv
import wx.grid
class GenericTable(wx.grid.PyGridTableBase):
def __init__(self,dat,rowL,colL):
wx.grid.PyGridTableBase.__init__(self)
self.data = dat
self.rowLabels = rowL
self.colLabels = colL
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0])
def GetColLabelValue(self, col):
if self.colLabels:
return self.colLabels[col]
def GetRowLabelValue(self, row):
if self.rowLabels:
return self.rowLabels[row]
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return self.data[row][col]
def SetValue(self, row, col, value):
pass
class SimpleGrid(wx.grid.Grid):
def __init__(self,parent, dat,rowL,colL):
self.data = dat
self.rowLabels = rowL
self.colLabels = colL
wx.grid.Grid.__init__(self,parent, -1)
sheet1= GenericTable(self.data,self.rowLabels,self.colLabels)
self.SetTable(sheet1)
self.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self.OnSelectedRange)
self.Bind
(wx.grid.EVT_GRID_SELECT_CELL,self.get_selected_cells)
self.currentSelection=[]
def OnSelectedRange(self,event):
if event.Selecting():
for index in range(event.GetTopRow(),event.GetBottomRow()
+1):
if index not in self.currentSelection:
self.currentSelection.append(index)
print self.currentSelection
else:
for index in range(event.GetTopRow(), event.GetBottomRow()
+1):
while index in self.currentSelection:
self.currentSelection.remove(index)
def get_selected_cells(self,event):
self.currentSelection=[event.GetRow(), event.GetCol()]
print self.currentSelection
event.Skip()
class Newt(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self,parent, -1, title, size = (1100, 600))
box = wx.BoxSizer(wx.VERTICAL)
menuBar = wx.MenuBar()
menu1 = wx.Menu()
menu1.Append(99,"&Open file","")
menu1.Append(101,"&Save file","")
menuBar.Append(menu1, '&File')
menu2 = wx.Menu()
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU,self.openfile,id=99)
toolbar1 = wx.ToolBar(self, -1, style= wx.TB_HORIZONTAL)
toolbar1.AddLabelTool(104, '', wx.Bitmap('/icons/document-
open.png'))
toolbar1.AddLabelTool(105, '', wx.Bitmap('/icons/drc-
icon.png'))
toolbar1.AddSeparator()
toolbar1.Realize()
self.Bind(wx.EVT_TOOL,self.openfile,id=104)
self.Bind(wx.EVT_TOOL,self.doseresponse,id=105)
box.Add(toolbar1, border=5)
box.Add((5,5) , 0)
self.SetSizer(box)
self.CreateStatusBar()
self.Centre()
self.Show(True)
def openfile(self,event):
dialogo=wx.FileDialog(self,"Choose file containing
data",os.getcwd(),"","*.*",wx.OPEN)
if dialogo.ShowModal()==wx.ID_OK:
self.pathb=dialogo.GetPath()
print self.pathb
dialogo.Destroy
self.readfile(self.pathb)
def readfile(self,event):
self.colL =
("1","2","3","4","5","6","7","8","9","10","11","12")
self.rowL = ("A", "B", "C", "D", "E", "F", "H", "I", "J")
invitro=open(self.pathb)
reader=csv.reader(invitro)
self.dat=list()
for row in reader:
t=tuple(row)
self.dat.append(t)
d=tuple(self.dat)
self.dat=d
print self.dat
self.notebook=wx.Notebook(self,-1,size=(1000,500), pos=
(10,50))
grid=SimpleGrid(self.notebook, self.dat,self.rowL,self.colL)
self.notebook.AddPage(grid, 'Sheet1')
self.notebook.Show()
def doseresponse(self,event):
panel=wx.Frame(self,-1,title="Selecting data rows and columns
of interest",size=(800,500))
basic=wx.StaticText(panel,-1,"Plese select the rows you would
like to work with:",pos=(20,20))
basic.SetFont(wx.Font(15,wx.DECORATIVE,wx.NORMAL,wx.BOLD))
####This is the part where I cannot update in real time what
the selection grid is for "self.currentSelection"
self.rowsel=wx.TextCtrl(self, -1,self.currentSelection,pos=
(100,100), size=(200,-1))
panel.Show(True)
app = wx.App()
Newt(None, -1, 'SpreadSheet')
Newt.Center
app.MainLoop()
The only comment in the whole code is where my question is: how do
I bind what is happening in Class 'Simple Grid' with the TextCtrl
appearing in 'panel' under the doseresponse function?
Thank you very much in advance for your help,
Judith