[Tkinter-discuss] Problem with event.widget.grid_info()

Michael Lange klappnase at web.de
Sun Dec 8 21:00:39 CET 2013


On Sun, 08 Dec 2013 11:18:38 +0000
Horace Stoica <fhstoica at googlemail.com> wrote:

> Hi,
> Not sure this is the place to submit such a query, please re-direct me 
> to the correct place.
> I implemented a simple Lights Out game which works with the old Python 
> 2.6 (and 2.7 in Windows).
> After upgrading to SUSE 13.1 which comes with Python 2.7.5 I get the 
> following error:
> *Exception in Tkinter callback**
> **Traceback (most recent call last):**
> **  File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1470, in
> __call__** **    return self.func(*args)**
> **  File "./LightsOut.py", line 28, in mouse**
> **    grid_info = event.widget.grid_info()**
> **  File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1978, in
> grid_info** **    self.tk.call('grid', 'info', self._w))**
> **TypeError: coercing to Unicode: need string or buffer, 
> _tkinter.Tcl_Obj found**
> *
> I get the same error with Python 3.3.2 (which also comes with SUSE 
> 13.1). I attached the source code for the game, please run with 
> "./LightsOut 3".

this is a bug in the installed Tkinter version, from time to time we
stumble across these TclObjects which are supposed to perform some
smart automagic behind the scenes to convert the strings that are returned
by calls to the Tcl interpreter into the appropriate Python object;
unfortunately sometimes this fails. Here apparently such a TclObject is
returned instead of a string.

There are two ways to work around this: 
- first you can, after you identified the problematic tk call, do the
TclObject to string conversion manually. Here this would probably mean
that you have to override Tkinter.Grid.grid_info().
- second, and this is what I prefer, you can turn the use of TclObjects
off altogether by simply calling once in your program, immediately after
the first "import Tkinter" but before creating the root window,

Tkinter.wantobjects = False

The disadvantage herein is that you might have to do some type
conversions yourself, because with wantobjects=False widget.tk.call()
will always return a string and unfortunately the tkinter developers
these days seem to rely on wantobjects being True. However you can be sure
that once your code runs you will be on the safe side with the next Python
version ;) 
The bad news however are that setting wantobjects to False
doesn't seem to work with Python3 any longer, at least this is the case
with Python 3.2.3 here on my debian wheezy system.



.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

I'm frequently appalled by the low regard you Earthmen have for life.
		-- Spock, "The Galileo Seven", stardate 2822.3

More information about the Tkinter-discuss mailing list