Interactive update of grid selection with wxTextCtrl

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

<snip>

def doseresponse\(self,event\):
    panel=wx\.Frame\(self,\-1,title=&quot;Selecting data rows and columns

of interest",size=(800,500))

You really shouldn't use a variable name like "panel" for a
"wx.Frame". That's confusing...

    basic=wx\.StaticText\(panel,\-1,&quot;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

Normally you can initialize the text control and the grid control in
the container class (i.e. a panel or frame). I'm not sure what you're
doing as it looks like you initialize a frame and a text control
inside a method. If you bind the cell select event, then you can set
the text control to whatever is in that cell in the handler for that
event. Otherwise, you might need pubsub.

- Mike

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:

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

I have attached an example that mostly works. For reasons that I don’t understand, grid.GetCellValue(row, col) seems to return the previously selected cell rather than the one that is currently selected. But if you click twice on the same cell, then you’ll get its contents. Anyway, the code does show the basic idea of what you should do. Tested on Windows XP with Python 2.5 and wxPython 2.8.10.1

grid-demo.py (2.05 KB)

···

On Wed, Oct 21, 2009 at 6:40 PM, Jury juryef@yahoo.com wrote:


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Thank you very much for you help Mike. I will not use ‘panel’ as an object name, you are right, it’s confusing.

Have a nice day!

Judith

···

From: Mike Driscoll mike@pythonlibrary.org
To: wxpython-users@googlegroups.com
Sent: Thu, October 22, 2009 8:34:01 AM
Subject: [wxPython-users] Re: Interactive update of grid
selection with wxTextCtrl

On Wed, Oct 21, 2009 at 6:40 PM, Jury juryef@yahoo.com wrote:

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:

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

I have attached an example that mostly works. For reasons that I don’t understand, grid.GetCellValue(row, col) seems to return the previously selected cell rather than the one that is currently selected. But if you click twice on the same cell, then you’ll get its contents. Anyway, the code does show the basic idea of what you should do. Tested on Windows XP with Python 2.5 and wxPython 2.8.10.1


Mike Driscoll

Blog: http://blog.pythonlibrary.org