in reply to Re^3: How to align the contents inside a ListboxSelect of Tk GUI
in thread How to align the contents inside a ListboxSelect of Tk GUI

Thanks again Ken, good tips about the method of getting dereference value. Yes, so now there are more fonts I can used than before, like 'Consolas', 'Courier','Lucida Console'.(https://en.wikipedia.org/wiki/Monospaced_font)
  • Comment on Re^4: How to align the contents inside a ListboxSelect of Tk GUI

Replies are listed 'Best First'.
Re^5: How to align the contents inside a ListboxSelect of Tk GUI
by kcott (Archbishop) on Oct 12, 2017 at 03:31 UTC

    Just a word of caution. Tk guarantees to support font families named Courier, Times, and Helvetica (see the '-family' option under "Tk::Font - FONT OPTIONS" for more specific details). From that section of the documentation, you can see that if you specify a font family that is unknown, you may get something that is unwanted, possibly even inappropriate: for example, you could get a serif font when you wanted a monospace font.

    In order for your application to look as close as possible to the way you want, I'd recommend specifying a list of font families, in order of preference, and picking the first one that's available on whatever system your GUI is running; put an appropriate supported font family at the end of the list. For this, you'll want the 'fontFamilies()' method (see "Tk::Font - DESCRIPTION").

    This is easy to do. I wrote two examples as one-liners; I've expanded them into multiple lines so you see the code more easily.

    In the first example, I've used the three families you showed: I don't have Consolas or Lucida Console available; Courier is picked as the best choice.

    $ perl -E ' use List::Util "first"; use Tk; my $mw = MainWindow->new; my @font_prefs = ("Consolas", "Lucida Console", "Courier"); my %font_have = map { fc $_ => 1 } $mw->fontFamilies; my $font_best = first { exists $font_have{fc $_} } @font_prefs; say $font_best ' Courier

    In the second example, I've added Luxi Mono to the list: this is a monospace font family that I do have, so it is chosen as the best choice.

    $ perl -E ' use List::Util "first"; use Tk; my $mw = MainWindow->new; my @font_prefs = ("Consolas", "Luxi Mono", "Lucida Console", "Cour +ier"); my %font_have = map { fc $_ => 1 } $mw->fontFamilies; my $font_best = first { exists $font_have{fc $_} } @font_prefs; say $font_best ' Luxi Mono

    I'd suggest putting code along those lines in a subroutine, or maybe even a separate module with font-related utilities. You'd only want to populate %font_have once. Perhaps call it with something like: "get_best_font($mw, $font_prefs_ref)".

    See also: List::Util::first(); and fc (that was introduced in v5.16[perl5160delta]; use lc or uc if you're writing for an earlier Perl version).

    — Ken