Determining whether a glyph is available in Tkinter

Terry Reedy tjreedy at udel.edu
Tue Dec 17 04:58:06 CET 2013


On 12/16/2013 6:59 PM, wmcbrine at gmail.com wrote:
> I'm not going to control the font.

Tk widgets that display text must use *some* font. If you do not select 
one, then you get the system-dependent default.
for k, v in tk.Text().configure().items(): print(k, v)
...

font ('font', 'font', 'Font', <font object: 'TkFixedFont'>, 'TkFixedFont')

TkTextFont may be preferable for your purposes, although on Windows both 
might be Courier New.

> This is for a program that's
> distributed to the general public, for use on a wide variety of
> systems. But what I do in the current version is to use the ASCII
> label strings by default, and have a command-line option to select
> the "graphical" (non-ASCII Unicode) labels. What I want is to make
> the graphical labels the default, and have the program detect, at
> runtime, whether any of the glyphs used in the fancy labels would
> render as "\uNNNN" in whatever the default font for the buttons is,
> and automatically revert to the ASCII labels in that case.

I would not assume that the default covers more than ascii.

But to answer your question, this might work: fonts have a measure() 
method that returns what the pixel length of a string would be if it 
were to be displayed.

 >>> import tkinter.font as tkf
 >>> ft=tkf.Font(font='TkFixedFont')
 >>> ft.metrics()
{'ascent': 17, 'descent': 5, 'fixed': 1, 'linespace': 22}
 >>> ft.measure('a')
12
 >>> ft.measure('\u9fff')
12

*If* the measure is, say, 50 or more for a character that would display 
as \uNNNN, then you would know. Go ahead and experiment.

> I'm assuming this is possible, because Tkinter itself seems to know
> which glyphs are unavailable, or they'd probably be showing up as
> those boxed number characters or question marks instead of "\uNNNN".

I do not know whether it is tk itself or a system graphics call that 
translates a sequence of codes to pixels on the screen.

-- 
Terry Jan Reedy




More information about the Python-list mailing list