Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^7: Insert checkbutton into MListbox

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


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

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; }


Comment on Re^7: Insert checkbutton into MListbox
Select or Download Code
Re^8: Insert checkbutton into MListbox
by ghosh123 (Monk) on Feb 07, 2012 at 09:00 UTC

    Thanks a lot. Tihs is exactly what I wanted.

      Hello, I am running into a small problem. In the end of your script if I print the %is_selected hash,I can get which all items are selected/deselected. Problem is when I am embedding your code in my module. Supoose I have a module(test.pm) which has a method InitDialog(). I am invoking InitDialog from some script by a test.pm object. Now this how I am embedding your code in test.pm. Here I am using Notebook because I need this textWidget-chkbtn-upDwn structure for different display style.

      package test; sub new { ## something } sub InitDialog { #something something my $book = $frame->Notebook->pack ; foreach my $type qw(pageone pagetwo pagethree) { $page = $book->add("$type",-label => $type); $text = $page->Scrolled('Text',-scrollbars => 'e', + -width => ($item_width+15), -background => '#efefe +f' )->pack; $text->tagConfigure('item', %attr_item); &build($text); } while(my ($k,$v) = each(%is_selected)) { print "$k -> $v \n"; } } sub build { # your same build subrtn # only difference is the passed $text argument is held # in a variable. The rest is same # And $text is passed to move as well } sub move { ## Same code &build($text); }

      Now when in sub OnInitDialog I am printing the has key-value, I am not getting any value for the has keys. It is empty. What is going wrong ??? Please help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://952107]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-07-31 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls