Some oddities in 2.3b1, esp. with Tkinter
Russell E. Owen
no at spam.invalid
Tue Apr 29 17:10:55 EDT 2003
I finally got my app almost running in 2.3b1. I've run into the
following:
1) A filter to remove fonts with non-ascii names (used to filter out
stuff that I could not display in a pop-up menu) broke.
try:
unicode(fontName, 'ascii', 'strict')
return True
except UnicodeError:
return False
Python 2.2 throws UnicodeError. Python 2.3b1 throws TypeError. The fix
is trivial (e.g. "except (UnicodeError, TypeError)". The filter is
probably not needed anymore anyway, since Tk 8.4.2 can apparently
display menu items with non-ascii characters.
2) Tk file events are no longer supported:
RuntimeError: _tkinter.createfilehandler not supported for threaded Tcl
This is painful since it's hard to see how to write a proper networking
GUI with Tkinter without file events for reading data. Perhaps I can get
them back again by disabling the new threaded tcl support. However, I
suspect it's time to try out the Twisted framework.
3) Tk returning things as objects is nice, but I had this break at least
one thing of mine and it will break others (see 4 below):
I was assuming that the values in the dictionary returned by
tkFont.Font.config() were strings. Fortunately this was easy to fix. A
bit disconcerting -- I wonder how many any other little pitfalls like
this still buried in my code -- but well worth the cost of the change.
4) Tk Variables are trickier to use and the change will break some
existing code.
Tkinter used to return a variable name if the variable of a widget was
requested. Not as convient as returning the original variable, but once
one found the magic setvar and getvar functions (which work on variable
names) things were fine.
Now Tk returns a _tkinter.Tcl_Obj instead, and and setvar and getvar
don't seem to work on that. (I'm relieved that I never actually used
setvar and getvar in my final code; despite many temptations I was
always uncomfortable with it).
Example:
v = StringVar()
v is a <Tkinter.StringVar instance at 0x2c98a0>
l = Label(textvariable=v)
x = l["textvariable"]
x is reported as a <namespaceVarName object at 0x0168b048>
but is a _tkinter.Tcl_Obj based on the following traceback:
root.getvar(x)
Traceback...
TypeError: getvar() argument 1 must be string, not _tkinter.Tcl_Obj
Note that x.get() doesn't work either -- i.e. the object does not mimic
a Tk Variable. I'm guessing there is some way to manage this, but have
no idea what it might be.
5) Note that Tkinter's handling of font objects does not seem to have
changed. I was hoping it would start returning font objects instead of
strings, or offer some way to convert the string to a font object, but
apparently not:
f = tkFont.Font(font="system")
l = Label(font=f)
l["font"] returns: 'font2922816'
-- Russell
More information about the Python-list
mailing list