Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^5: Insert checkbutton into MListbox

by chessgui (Scribe)
on Feb 06, 2012 at 12:47 UTC ( #952081=note: print w/replies, xml ) Need Help??

in reply to Re^4: Insert checkbutton into MListbox
in thread Insert checkbutton into MListbox

use Tk; my $mw=new MainWindow; my $item_width=15; my %attr_item=('-background'=>'#ffafaf'); my %attr_btn=('-background'=>'#afffaf','-activebackground'=>'#7aaa7a') +; my $text=$mw->Scrolled('Text', -insertontime=>0, -scrollbars=>'e', -width=>($item_width+15), -background=>'#efefef' )->pack; my $first=1; $text->tagConfigure('item',%attr_item); foreach my $item ("State" , "Dependency", "CalcTime", "Result") { my $btn=$text->Checkbutton(%attr_item,'-activebackground'=>'#ff7f7 +f'); my $up=$text->Button(-text=>'Up',%attr_btn); my $down=$text->Button(-text=>'Down',%attr_btn); $text->insert('1.0',"\n") unless $first; $first=0; $text->windowCreate('1.0',-window=>$up); $text->windowCreate('1.0',-window=>$down); $text->windowCreate('1.0',-window=>$btn); $text->insert('1.0',sprintf('%-'.$item_width.'s',$item)); $text->tagAdd('item','1.0',"1.$item_width"); } MainLoop;

Replies are listed 'Best First'.
Re^6: Insert checkbutton into MListbox
by ghosh123 (Monk) on Feb 06, 2012 at 14:11 UTC

    This was really helpful. I was also thinking of using hash for storing and re-positioning the text items upon clicking on up and down button. I proceeded as below but a bit stuck. Let us assume I have put your code in a subroutine called build_layout()

    my $dynamic_hash = {0 => "job",1=> "state",2 =>"dependencies",3=>"resu +lt"}; &build_layout($init_hash); sub build_layout { ## samething what u have sent foreach my $itemkey (sort keys %$dynamic_hash) { my $btn=$text->Checkbutton(%attr_item,'-activebackground'=>'#ff7f7 +f' ); my $up=$text->Button(-text=>'Up',%attr_btn, -command => [\&moveup, +${$dynamic_hash}{$itemkey} ); my $down=$text->Button(-text=>'Down',%attr_btn,)-command => [\&mov +edown,${$dynamic_hash}{$itemkey}; ..... ..... $text->insert('1.0',sprintf('%-'.$item_width.'s',${$dynamic_hash}{ +$itemkey})); } sub moveup { my $text = shift ### not able to proceed here } sub movedown { }
      Note that I've added hash %is_selected for the Checkbuttons to have a 'memory' ( $is_selected{$item} will tell you whether the given item is required ). The moving of Buttons is achieved with manipulating the list itself (no hash necessary for that):
      use Tk; my @items=qw(State Dependency CalcTime Result FooState FooDependency F +ooCalcTime FooResult BarState BarDependency BarCalcTime BarResult); my %is_selected; my $mw=new MainWindow; my $item_width=15; my $UP=1; my $DOWN=-1; my %attr_item=('-background'=>'#ffafaf'); my %attr_btn=('-background'=>'#afffaf','-activebackground'=>'#7aaa7a') +; my $text=$mw->Scrolled('Text', -insertontime=>0, -scrollbars=>'e', -width=>($item_width+15), -background=>'#efefef' )->pack; my $first=1; $text->tagConfigure('item',%attr_item); &build; MainLoop; sub build { $text->delete('1.0','end'); foreach my $item (@items) { my $btn=$text->Checkbutton(%attr_item, -variable=>\$is_selected{$item}, -onvalue=>1, -offvalue=>0, '-activebackground'=>'#ff7f7f' ); my $up=$text->Button(-text=>'Up',-command=>[\&move,$item,$UP],%att +r_btn); my $down=$text->Button(-text=>'Down',-command=>[\&move,$item,$DOWN +],%attr_btn); $text->insert('1.0',"\n") unless $first; $first=0; $text->windowCreate('1.0',-window=>$up); $text->windowCreate('1.0',-window=>$down); $text->windowCreate('1.0',-window=>$btn); $text->insert('1.0',sprintf('%-'.$item_width.'s',$item)); $text->tagAdd('item','1.0',"1.$item_width"); } } sub move { my $what=shift; my $dir=shift; my ($index)=grep {$items[$_] eq $what} (0 .. $#items); my $new_index=$index+$dir; $new_index=0 if($new_index>=@items); $new_index=@items-1 if $new_index<0; my $temp=$items[$new_index]; $items[$new_index]=$items[$index]; $items[$index]=$temp; &build; }

        Thanks a lot. Tihs is exactly what I wanted.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://952081]
and the daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-06-18 19:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.