Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

TK font control Linux Windows

by boleary (Scribe)
on Oct 03, 2019 at 13:24 UTC ( #11107007=CUFP: print w/replies, xml ) Need Help??

I've been struggling for a couple of years with the differences between the Linux and Windows versions of my TK based perl tools.
So one of my TK UIs in windows would fit nicely on a 1920x1080 screen,
but would expand strangely and overflow the display when opened in Linux.
It would take me several hours to re-layout the GUI to work with both applications
It turns out that the main problem was default font sizes.

The default Linux font was Helvetica 12 for most widgets, and the Windows font is Arial 8.
Once I figured that out, I looked around for some solutions to controlling the font sizes.
The Mastering TK books talk about using Xdefaults or TK::CmdLine,
but I am distributing my tools as wrapped executables and I cannot count on any kind of pre-existing environment

I finally found this link from the TCL wiki

It was close to what I wanted (in TCL-TK ) But I wanted to have a little more control and set all the Label type widgets to use bold text
and all the entry type widgets with the same font but not bold

I came up with this solution that I wanted to share (and be able to search for later) I wrote a sub to defined the default fonts for my application
and then I call that sub immediately after creating my MainWindow

#main application our $MAIN_WINDOW=MainWindow->new; #make call here to init the fonts from the very beginning &init_mw_fonts($ctlRef,$MAIN_WINDOW); $MAIN_WINDOW->title("CadEnhance:Part Builder Schematic Symbol Creator" +); $MAIN_WINDOW->configure(-menu=>my $menubar=$MAIN_WINDOW->Menu); # # # MainLoop; sub init_mw_fonts{ #set the default font types or all of our widget classes my $ctlRef=shift; my $mw=shift; require allToolCtlVariables; my $boldFont =&my_tk_cfg_vars("BOLD_FONT") || &all_tk_cfg_vars("BOLD +_FONT"); my $normalFont =&my_tk_cfg_vars("NORMAL_FONT") || &all_tk_cfg_vars(" +NORMAL_FONT"); my $textFont =&my_tk_cfg_vars("TEXT_FONT") || &all_tk_cfg_vars("TEXT +_FONT"); #first set ALL fonts to $normalFont $mw->optionAdd("*font",$normalFont); #then we pick and choose the items we want to be bold foreach my $boldClass (qw(Checkbutton Label Listbox Button Menu Menubutton Message Radiobutton Scale + ProgressBar)) { $mw->optionAdd("*$boldClass.font",$boldFont); } #now set the text Font $mw->optionAdd("*Text.font",$textFont); } #This subroutine lets us program the fonts we want to use #and we can create a similar one for each application called #my_tk_cfg_vars which will override the global one sub all_tk_cfg_vars { my $selection=shift; my %gui_vars=( ENTRY_WIDTH=>80, CFG_ENTRY_WIDTH=>40, DT_ENTRY_WIDTH=>98, TEXT_WIDTH=>110, BACK_GROUND_COLOR=>"light grey", YPAD_SMALL=>0.5, YPAD_MED=>1.0, YPAD_LARGE=>5.0, XPAD_SMALL=>0.5, XPAD_MED=>1.25, XPAD_LARGE=>15, LABEL_FONT=>"Arial 9 bold", BOLD_FONT=>"Arial 9 bold", NORMAL_FONT=>"Arial 9", TEXT_FONT=>"Helvetica 10", ); if (defined ($gui_vars{$selection})) { return $gui_vars{$selection}; } else { return 0; } } sub my_tk_cfg_vars{ my $param=shift; return ""; #or override the values defined in all_tk_cfg_vars }

Hopefully this may help someone else who might be hitting the same issue

Replies are listed 'Best First'.
Re: TK font control Linux Windows
by rjt (Curate) on Oct 03, 2019 at 13:35 UTC

    Since you're sharing code and not asking a question, I've moved this to CUFP. I salute your cross-platform tenacity. I'd have given up and spit out HTML years ago...

      HTML vs TK?
      Are you using HTML as an interactive user interface?
      I have switched much of my reporting to HTML, but not the control and editing.

      I ask this as I have already commissioned someone to start working on a chromium based JavaScript interface to work around the turtle like speed of the TK Hlist widget.
      the problem is it takes me out of the development path since its a big learning curve

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11107007]
Approved by rjt
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2023-05-30 14:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found