in reply to Re: 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, I just fix it by replacing the original default font with mono font. and curiosity I also want to know what the default font style name is?
  • Comment on Re^2: How to align the contents inside a ListboxSelect of Tk GUI

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

    G'day Evel,

    "... I also want to know what the default font style name is?"

    You can use the cget() method (see Tk::options) to determine the default option value of any widget.

    You can find values quickly using a one-liner, e.g.

    $ perl -E 'use Tk; say MainWindow->new->Listbox->cget("-background")' #d9d9d9

    Some option values aren't individual scalars: you may get a reference returned, e.g.

    $ perl -E 'use Tk; say MainWindow->new->Listbox->cget("-font")' Tk::Font=SCALAR(0x7f972e1bbd80)

    You can just dereference as normal. Here's two examples showing the older "${ ... }", and newer "...->$*", syntaxes. See "perlref: Postfix Dereference Syntax" if you're unfamiliar with the second one.

    $ perl -E 'use Tk; say ${ MainWindow->new->Listbox->cget("-font") }' Helvetica -12 bold $ perl -E 'use Tk; say MainWindow->new->Listbox->cget("-font")->$*' Helvetica -12 bold

    Here's another example just to show different widgets have different defaults.

    $ perl -E 'use Tk; say MainWindow->new->Text->cget("-font")->$*' Courier -12

    Also be aware that, while Tk has its own defaults, these may be altered externally (e.g. a .Xdefaults file); or from within your application by a module you've loaded (e.g. a custom widget), or by your own code (e.g. one of the "option*()" methods). See Tk::option for details.

    — Ken

      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)

        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