swe9 has asked for the wisdom of the Perl Monks concerning the following question:

I have a global mode for my program that may be on or off. I would like to manage and indicate the state from the top level of the (Tk 8.0 style) menubar. I tried putting a checkbutton into the menubar and it sets its variable appropriately. However, it does *not* show its state :-(

For the moment, I've used a cascade that then includes a checkbutton to show the state. Perhaps a checkbutton in the menubar is inelegant, but this seems even worse...

Is there a better or "right" way to handle a global state indicator like this? I would be happy enough if the menubar allowed the checkbox to show up to indicate the state.

Replies are listed 'Best First'.
Re: Menubar checkbutton to show state?
by stefbv (Priest) on Aug 19, 2010 at 06:35 UTC

    I assume you use Perl Tk, in this case you can use a Tk::StatusBar widget for the task.

    Here is a tiny patch to fix a problem, I encountered, regarding 'systembuttonface':

    --- lib/Tk/StatusBar.pm 2004-02-05 05:21:14.000000000 +0200 +++ lib/Tk/StatusBar.pm.new 2010-03-29 12:58:51.164978272 +0300 @@ -147,7 +147,6 @@ my $n = $self->ProgressBar( -borderwidth => $borderwidth, -width => 17, - -troughcolor => 'systembuttonface', %args, );

    Update: How would I do it: :)

    use strict; use warnings; use Tk; use Tk::StatusBar; use Tk::ToolBar; my $mw = MainWindow->new; $mw->geometry('200x80+10+20'); $mw->optionAdd('*Label.font' => '{MS Sans Serif} 8'); my $tb = $mw->ToolBar(qw/-movable 1 -side top/); my $connected = 1; $tb->ToolButton( -image => 'actreload16', -tip => 'back', -command => \&toggle_conn, ); my $sb = $mw->StatusBar(); my $txtstatus = $sb->addLabel( -relief => 'flat' ); my $icostatus = $sb->addLabel( -width => 20, -relief => 'raised', -anchor => 'center', -side => 'right' ); toggle_conn(); MainLoop(); sub toggle_conn { if ($connected) { $connected = 0; my $text = 'Disconnected'; $icostatus->configure( -image => 'connectno16' ); $txtstatus->configure( -foreground => 'darkred', -textvariable => \$text, ); } else { $connected = 1; my $text = 'Connected'; $icostatus->configure( -image => 'connectyes16' ); $txtstatus->configure( -foreground => 'darkgreen', -textvariable => \$text, ); } }

    Regards, Stefan

Re: Menubar checkbutton to show state?
by zentara (Cardinal) on Aug 19, 2010 at 13:14 UTC
    To be honest, Tk is not very good at accessing the window manager's statusbar, but Gtk2 is. The example below, is a Tk program, which you can place wherever you want as an indicator. For Gtk2, see Gtk2::StatusIcon Demo ( which may not work on all WindowManagers :-( )
    #!/usr/bin/perl use Tk; use strict; my $mw = MainWindow->new; $mw->geometry('20x20-20-20'); $mw->overrideredirect(1); # make right mouse button click an exit $mw->bind( '<3>' => sub{ exit } ); my @color = qw/red lightgray/; my $bits = pack("b8"x8, "...11...", "..1111..", ".111111.", "11111111", "11111111", ".111111.", "..1111..", "...11...",); $mw->DefineBitmap('indicator' => 8,8, $bits); my $label = $mw->Label(-bitmap=>'indicator', -fg=>'red', -bg => 'black +') ->pack( -fill=>'both', -expand=>1); $mw->repeat(500,sub{$label->configure( -fg=>$color[0]); @color=reverse(@color); }); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      Thanks for the responses! I'll have to try things out and see if I can make it work.