Tkinter special math chars

Jeff Epler jepler at unpythonic.net
Thu May 19 12:44:45 EDT 2005


I wrote the following code:
    import Tkinter
    t = Tkinter.Label()
    t.configure(
        text=u"As the function approaches \N{INFINITY}, \N{HORIZONTAL ELLIPSIS}")
    t.pack()
    t.mainloop()
It worked for me on Windows NT 4.0 with Python 2.4, and on RedHat 9 with
a self-compiled Python 2.3, showing an infinity symbol and an ellipsis.

u'\N{...}' stands for the Unicode character named '...'.  Unicode.org
(and other sites) have lists of Unicode character names. 

Tk tries very hard to find the requested character in some font
available on the system, but when it fails it just displays a unicode
escape sequence like "\u220e" (instead of the END OF PROOF symbol, in
this case), and there's really no way for Python to find out and fall
back in some graceful way.

Relying on this behavior, here's a somewhat-falliable way to detect the
presence of a symbol in the font used in a given widget:
    def symbol_exists(s, w, f = None):
        if f is None:
            f = w.cget("font")
        width_symbol = w.tk.call("font", "measure", f, s)
        width_bench = w.tk.call("font", "measure", f, "000")
        return width_symbol < width_bench
This finds the width in pixels of the given symbol (s) and the string "000", in
the font f.  If the width of the symbol is smaller, then it's probably available.
If it's wider, then it's probably rendered as an escape sequence like "\u220e".
This is falliable because there's no guarantee that the symbol would not be as
wide as 000, but also it's possible for some escape code (say \u1111) to be
narrower than 000.  Neither of these seem very likely in practice.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050519/e3a6ec94/attachment.sig>


More information about the Python-list mailing list