Fix for a Tktable bug....

jerry.levan at gmail.com jerry.levan at gmail.com
Tue Jul 11 21:41:07 EDT 2006


jerry.levan at gmail.com wrote:
> jerry.levan at gmail.com wrote:
> > jerry.levan at gmail.com wrote:
> > > Hi,
> > > I have a Tktable object (self.table) and when I click on a row the
> > > whole row is selected.
> > >
> > > If I click of a button to get the row contents then
> > >
> > > self.table.curselection() fails with a traceback of:
> > >
> > > Traceback (most recent call last):
> > >   File
> > > "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/lib-tk/Tkinter.py",
> > > line 1345, in __call__
> > >     return self.func(*args)
> > >   File "/Users/jerry/python/PyPgExplorerUni/Resources/editor.py", line
> > > 159, in deleteRow
> > >     print self.table.curselection()
> > >   File
> > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/Tktable.py",
> > > line 139, in curselection
> > >     return self._getCells(self.tk.call(self._w, 'curselection'))
> > >   File
> > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/Tktable.py",
> > > line 106, in _getCells
> > >     for i in string.split(cellString):
> > >   File
> > > "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/string.py",
> > > line 292, in split
> > >     return s.split(sep, maxsplit)
> > > AttributeError: 'tuple' object has no attribute 'split'
> > >
> > > It is my reading that curselection will return the indices of the
> > > selected cells...
> > >
> > > Help,
> > >
> > > Jerry
> >
> > looking into Tktable the curselection function is defined by:
> >
> >     def curselection(self, setValue = None):
> > 		if setValue != None:
> > 			self.tk.call(self._w, 'curselection', 'set', setValue)
> > 		else:
> > 			return self._getCells(self.tk.call(self._w, 'curselection'))
> >
> > looking at getCells we see:
> >
> >     def _getCells(self, cellString):
> >               #JHL
> >               print cellString
> > 		res = []
> > 		for i in string.split(cellString):
> > 			res.append(tuple(map(int, string.split(i, ','))))
> > 		return res
> >
> > When I run my program and select a row the value of cellString is
> >
> >        ('3,0', '3,1', '3,2', '3,3', '3,4', '3,5', '3,6')
> >
> > Which is the correct results...( ie the "fourth" row is selected and
> > there are six
> > columns...
> >
> > Unfortunately the above is *not* a string and hence the "for" statement
> > fails.
> >
> > It is hard to believe that I am the first person to attempt to retrieve
> > a selection
> > from a Tktable object....
> >
> > Am I overlooking something obvious?
> >
> > Jerry
>
> I think I have found a bug in the Tktable.py wrapper.
>
> It appears that if a row is selected in a Tktable (in Python)
> then any attempt to fetch the selection with curselection
> fails when a string operation is performed on a "tuple"
>
> the relevant routines in Tktable.py are:
>
>
>    def curselection(self, setValue = None):
> 		if setValue != None:
> 			self.tk.call(self._w, 'curselection', 'set', setValue)
> 		else:
> 			return self._getCells(self.tk.call(self._w, 'curselection'))
>
> curselection will call _getCells
>
>     def _getCells(self, cellString):
> 		res = []
>                 #JHL
>                 if type(cellString) == type(()) : return cellString
> 		for i in string.split(cellString):
> 			res.append(tuple(map(int, string.split(i, ','))))
> 		return res
>
> I added the "if type( ...." line to prevent the error.
>
> If a table row is selected cellString will look like
>
> ('3,0', '3,1', '3,2', '3,3', '3,4', '3,5', '3,6')
>
> which is of type "tuple" which fails the string.split command ;(
>
> I suspect it is probably best to just short circuit the call to
> _getCells.
>
> The following routine (with the above patch )will correctly retrieve
> the contents
> of a selected row.
>
>    def deleteRow(self):
>         res = self.table.curselection()
>         for index in res :
>             print self.table.get(index)
>
> Tain't clear to me where this should be reported
>
> Jerry

It would not be any fun for my app users if I had to change the
Tktable.py
source all by myself....

Here is another fix that involves calling Tk directly

instead of :
   res = self.table.curselection()

I can do:
  res = self.root.tk.call(self.table,'curselection')

root is the app top level ( self.table works also...)

for a row  the above call will return something like
   ('2,0', '2,1', '2,2', '2,3', '2,4', '2,5', '2,6')

the "co-ordinate" strings in the above tuple can be used
as indices into the table to retreive the selection.


Jerry




More information about the Python-list mailing list