Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Verbose Tk Code

by jcb (Parson)
on Mar 01, 2021 at 04:48 UTC ( [id://11128929] : note . print w/replies, xml ) Need Help??


in reply to Verbose Tk Code

As another monk mentioned, you can create the widgets, place them into an array and then apply geometry management to the array elements, but the packer is really a particularly simple case — the other geometry managers end up needing more information, often unique to each widget.

As for -background => 'cyan', Tk offers an "options database" that uses something similar to app-defaults values and the options database should be used in preference to hardcoded color schemes. I am still learning how to use this and would appreciate any help other monks can provide for all here, but here are some bits from a program I am working on: (partially developed; partially tested; this is from the program I mentioned in POE::Filter for Tor control protocol)

use FindBin qw($Bin); # ... $poe_main_window->configure(-title => 'Tor Monitor'); $poe_main_window->appname('TorMon'); $poe_main_window->iconname('TorMon'); $poe_main_window->optionReadfile(File::Spec->catfile($Bin, 'app-defaul +ts'), 'startupFile'); $poe_main_window->configure (-background => $poe_main_window->optionGet('TorMon.background', 'Top'));

$Bin/app-defaults:

! Main TorMon.background: ghost white

So far, I have had no trouble applying defaults from an options file to all other widgets created (use "*" instead of "." in the options file if you want a setting to apply to a widget's descendants), but the main window itself just does not seem to "pick up" the settings from the options database. Lines 9 through 11 in the example is the workaround that I have been using. Maybe this is an interaction between Tk and POE?

You may also find the Name and -class options to be useful here; they are analogous to the id and class attributes in HTML if you consider the options database analogous to a stylesheet.

Lastly, setting the iconname on the main window is important — some window managers can crash while manipulating windows that do not set an icon name.

Replies are listed 'Best First'.
Re^2: Verbose Tk Code
by bliako (Monsignor) on Mar 01, 2021 at 08:41 UTC

    re: app-defaults: that sounds exactly like ~/.Xdefaults -- yet another DSL.

      It basically is — the same format is used for both defaults (app-defaults files in traditional Xaw programs) and customizations (.Xdefaults and the "resource database"). As far as I can tell, Tk picked it up from Xrm, and there actually is an option to have Tk use Xrm but I have not looked into that yet.

Re^2: Verbose Tk Code
by parv (Parson) on Mar 01, 2021 at 06:02 UTC
    setting the iconname on the main window is important some window managers can crash while manipulating windows that do not set an icon name.

    I find that rather hilarious, not funny but ridiculing-the-wm kind.