Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Display new widgets after MainLoop

by swe9 (Initiate)
on Aug 11, 2010 at 16:03 UTC ( #854437=perlquestion: print w/replies, xml ) Need Help??
swe9 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to allow a directory to be selected (after MainLoop has been called) and then populate a grid managed frame with widgets based on the directory contents. I don't see any method to do that... HList has add() and ListBox has insert() - is there an equivalent for Frame? Is there another widget for this that can be managed by grid?

Replies are listed 'Best First'.
Re: Display new widgets after MainLoop
by shmem (Chancellor) on Aug 11, 2010 at 16:53 UTC
    (after MainLoop has been called)

    Not after the MainLoop. This loop never exits (if you don't destroy the MainWindow, that is).

    Instead, you bind events to code (subroutines), which does what you want (e.g. create widgets). Then if a event is triggered, the code bound to this event is executed.

    populate a grid managed frame

    You just create those widgets as children of the frame:


    where $Widget is Button, Frame, Entry, Canvas, ... etc.

      It's been a while since TK for me..but i do remember that had to re-pack parent container under certain circumstances to get widgets to update.
      the hardest line to type correctly is: stty erase ^H
Re: Display new widgets after MainLoop
by zentara (Archbishop) on Aug 11, 2010 at 16:49 UTC
    Try HList, just put the code below to open in a new toplevel, or frame. Also check out a wacky widget I made at Tk-CanvasDirTree, which is a dynamic tree on a canvas. In the below example, you would put the HLists into a Scrolled::Pane, but this simple example just dumps them adjacent to the last in a simple Frame.
    #!/usr/bin/perl use strict; use Tk; use Tk::HList; my $mw = MainWindow->new(); $mw->geometry("500x500+100+100"); my $h; #create some sample data my %data; foreach (0..100) { $data{$_}{'name'} = 'name'.$_; $data{$_}{'id'} = rand(time); } #get random list of keys my @keys = keys %data; ################# my $button0 = $mw->Button(-text => 'show list', -command => sub{ showlist() } )->pack; my $button = $mw->Button(-text => 'exit', -command => sub{exit})->pack; my $sortid = $mw->Button(-text => 'Sort by Id', -command => [\&sort_me,1] )->pack; MainLoop; ######################################################### sub showlist{ $h = $mw->Scrolled( 'HList', -header => 1, -columns => 2, -width => 30, -height => 60, -takefocus => 1, -background => 'steelblue', -foreground =>'snow', -selectmode => 'single', -selectforeground => 'pink', -selectbackground => 'black', # -browsecmd => \&browseThis, )->pack(-side => "left", -anchor => "n"); my $nameh = $h->header('create', 0, -text => ' Name ', -borderwidth => 3, -headerbackground => 'steelblue', -relief => 'raised'); my $idh = $h->header('create', 1, -text => ' ID ', -borderwidth => 3, -headerbackground => 'lightsteelblue', -relief => 'raised'); foreach (@keys) { my $e = $h->addchild(""); #will add at end $h->itemCreate ($e, 0, -itemtype => 'text', -text => $data{$_}{'name'}, ); $h->itemCreate($e, 1, -itemtype => 'text', -text => $data{$_}{'id'}, ); } } sub sort_me{ my $col = shift; my @entries = $h->info('children'); my @to_be_sorted =(); foreach my $entry(@entries){ push @to_be_sorted, [ $h->itemCget($entry,0,'text'), $h->itemCget($entry,1,'text') ]; } my @sorted = sort{ $a->[$col] cmp $b->[$col] } @to_be_sorted; my $entry = 0; foreach my $aref (@sorted){ # print $aref->[0],' ',$aref->[1],"\n"; $h->itemConfigure( $entry, 0, 'text' => $aref->[0] ); $h->itemConfigure( $entry, 1, 'text' => $aref->[1] ); $entry++; } $mw->update; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      I looked at HList but it wasn't clear that I could easily create a scrolling grid of thumbnails with it. I want a rectangular area with rows and columns of thumbnails that I can select and operate on. (Ultimately I'd like to be able to remove thumbnails as operations are completed but that's for later.)
        You can use Hlist for thumbnails, see ztkdb and ztkdb-sql

        HList is the most configurable list in Tk.

        You can also use Gtk2's various list widgets, which accept pixbuf fields.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
Re: Display new widgets after MainLoop
by sierpinski (Chaplain) on Aug 11, 2010 at 16:33 UTC
    Can you post the appropriate code so we can get a bigger picture of what you're trying to do?
      In my callback I have a call to this subroutine:
      sub ThumbNail { my($name) = @_; my $row = $img_id % 4; my $col = $img_id - 4 * $row; my $img = $frame->Photo(-format => 'jpeg', -file => $name); my $iratio = $img->width()/200; my $nh = $img->height()/$iratio; my $newimg = $frame->Photo(-width => 200, -height => $nh); $newimg->copy($img, -subsample=>($iratio, $iratio)); my $imgL = $frame->Label('-image' => $newimg)->pack; $imgL->bind('<Button-1>', [\&doFocus, $imgL] ); $imgL->bind ( '<Key-1>' , [\&rate, $img_id, 1]); $imgL->bind ( '<Key-2>' , [\&rate, $img_id, 2]); $imgL->bind ( '<Key-3>' , [\&rate, $img_id, 3]); $imgL->bind ( '<Key-4>' , [\&rate, $img_id, 4]); $imgL->bind ( '<Key-5>' , [\&rate, $img_id, 5]); @imgs[$img_id] = { name => $name, lbl => $imgL, sel => 0 }; $img_id++; return $imgL; }
      Some command windows flicker and then it aborts.
Re: Display new widgets after MainLoop
by Anonymous Monk on Aug 12, 2010 at 07:09 UTC
    Using the command window and -W I found that there were errors messing up the code. This wasn't a widget display problem after all. I still get flashing command windows for every image created...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://854437]
Approved by sierpinski
[Lady_Aleena]: I did it! I did it! I did it! # it eq made a page better with a browse feature instead of listing everything at once.
Lady_Aleena feels so good about it.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-05-28 07:30 GMT
Find Nodes?
    Voting Booth?