Determining whether a glyph is available in Tkinter

W

wmcbrine

I have a Tkinter app that can optionally label some buttons with certain Unicode glyphs that aren't always available (depending on the OS, etc.). Whenthey aren't available, Tkinter renders them as "\uNNNN". What I'd like to do is check whether the glyphs are available, and fall back to my own alternate text for the button if not. Can I do this?

I'd also like to do the same in pygtk.
 
T

Terry Reedy

I have a Tkinter app that can optionally label some buttons with
certain Unicode glyphs that aren't always available (depending on the
OS, etc.).

It depends on the font in use. The best scenario would be to always use
the same unicode font. Idle, built on tkinter, has a configuration
dialog that gets a list of available fonts and sets the one the user
selects. I use Lucida Sans Unicode. It is not very pretty, but it seems
to cover much the BMP. AFAIK, it should be available on all Windows from
XP on. I do not know what comes with *nix and mac, but from reading
https://en.wikipedia.org/wiki/Unicode_font#List_of_Unicode_fonts
there seem to be TrueType fonts that you could install with your
software: FreeSerif (etc), GNU Unifont, (both GPL), BitstreamCyber (free
for non-commercial use). The font table linked above is followed by
table indicating something about coverage. Unifont is the champ.
When they aren't available, Tkinter renders them as
"\uNNNN". What I'd like to do is check whether the glyphs are
available, and fall back to my own alternate text for the button if
not. Can I do this?

Don't know. See the 'Utility software' section of the page above.
I'd also like to do the same in pygtk.

No idea.
 
W

wmcbrine

I'm not going to control the font. This is for a program that's distributedto the general public, for use on a wide variety of systems. But what I doin 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'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".
 
T

Terry Reedy

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.
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.
 
W

wmcbrine

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

In this case, I already know that the glyphs I chose work with the default fonts for OS X 10.4+ and Windows 7+, but not for (for example) Win XP.
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.

Thanks. Unfortunately, measure() doesn't seem to do the trick -- it's giving me answers that look like normal single-width characters, for glyphs that actually render as \uNNNN. Oh well. :/
 
W

wxjmfauth

Le vendredi 20 décembre 2013 00:10:58 UTC+1, (e-mail address removed) a écrit :
On Monday, December 16, 2013 10:58:06 PM UTC-5, Terry Reedy wrote:
In this case, I already know that the glyphs I chose work with the default fonts for OS X 10.4+ and Windows 7+, but not for (for example) Win XP.

As I pointed in an another thread, Windows 7 is the
first Windows which became full unicode compliant
(0th order approximation).

If your application works fine with win7, good. If it
does not on XP, it's like this.


---

Font: I do not know what glyphs you "need". Do
not expect to find always a single font that will
cover your needs.

It's a little bit a side effect of "unicode", but
everything has been constructed to be a no problem.
And it is a no problem.

jmf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top