ak.arjun has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using Perl/Tk to build a gui application and i need some advice. my issue is that i want to use a check-button in three states, i.e. i want to have a partial selection mode for check-button. But tk provide check-button with only two states.

Now with each check-button there is a variable associated on changing the value for that variable reflects the change in the corresponding check-button. What i want to do is alter this part of in such way that this variable can operate on three values rather than two(current behavior). What i mean is that instead of just having two values associated to that variable , i want to associate a third value in which i will change the select color property. Can this be done???

Replies are listed 'Best First'.
Re: Tk Checkbutton
by zentara (Cardinal) on Apr 24, 2012 at 12:12 UTC
    You would be better off making your own widget on a sub-classed Canvas. However you could bind a right mouse button click to each checkbutton, to simulate 3 states.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my @states = qw(Alaska Hawaii Michigan Georgia Arizona Washington Oreg +on California Texas Utah Minnesota); my $mw = tkinit(); $mw->geometry('300x300+100+100'); my $sp = $mw->Scrolled('Pane', -scrollbars=>'osoe', sticky=>'nwse') ->pack(-expand=>1, -fill=>'both' ); my %cbs; my @cbvalues; my @cbnames; my $count = 0; foreach my $d( @states){ $cbnames[$count] = $d; $cbs{$count} = $sp->Checkbutton(-text => $d, -font=>[arial => 12], -onvalue => 1, -offvalue => 0, -variable => \$cbvalues[$count], -font => 'big', -bg => 'white', )->pack(-anchor=>'w')->pack(); $cbs{$count}->bind('<ButtonRelease-3>', sub{ print "@_\n"; print "do you 3rd state thing here\n"; }); $count++; } my $showbutton= $mw->Button(-text=>'Show Selected', -bg => 'lightyellow', -command => sub{ my @selected = (); foreach my $c( 0.. $count ){ if ( $cbvalues[$c] ){ push @selected, $cbnames[$c]; } } print "@selected\n"; } )->pack(); MainLoop();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Well making my own widget is an option, and can't use right mouse click because my requirement is to make a hierarchical tree structure with each entry of tree containing a check-button. Hence whenever each check-button is selected all its child node should get selected, but when de-select a check-button, the parent check-button will get partially de-selected(third state) or or totally de-selected(only if all its child node are de-selected))

      What i was thinking that each check-button has a variable associated to it, and when i change the value of that variable to on/off state, the check-button gets selected/de-selected automatically. So if i know how does tk is exactly implementing this behavior, i can make changes in that area of code to add my functionality. Is it possible?

Re: Tk Checkbutton
by Anonymous Monk on Apr 24, 2012 at 12:03 UTC

    Not with a checkbutton, they're meant to be on or off

    You can however create your own button class like Tk::StyledButton or Tk::ImageButton but with more states than normal/active/disabled