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


In reply to Re^5: How to align the contents inside a ListboxSelect of Tk GUI by kcott
in thread How to align the contents inside a ListboxSelect of Tk GUI by Evel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.