Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating multiple windows and entry boxes at run-time with Perl/Tk 1

Status
Not open for further replies.

comma

Programmer
Feb 12, 2003
24
FI
Hi!

I'm little new to (Perl/)Tk and was wondering if it's possible to create new windows in runtime or create entry boxes in run time when needed.

In Perl/Tk, when the MainLoop() is called the UI is launched and it means that the building of widgets is done.

I'm trying to create user interface for XML-file (some data stored in XML which is modified in UI) contents editing. The content is divided into categories and it would be nice to have menu choices based on categories (done already) and by clicking desired category a new window would open displaying the data in entryboxes.

Do you have any input or good links?

Thank you!

br, comma
 
If I understand correctly, just create a sub-routine containing either a top-level or new window and call the sub whenever you need it.
 
You don't have to create all of your widgets at runtime. You can create them on-the-fly, when and as you need them. The code below is simple illustration of creating two different label widgets depending on which button you push. The label widgets are not created above 'MainLoop', instead are created and 'packed' in the sub routines which are attached to the buttons.

Code:
#!perl
use strict;
use Tk;

my $second_widget;
my $third_widget;

my $mw1 = MainWindow->new;
$mw1->configure(-title=>"NOAA Coastal Services Center CD Images Writer");
#                w x h
$mw1->geometry('640x420+100+100');

my $menuFrame = $mw1->Frame(-relief=>'ridge', -borderwidth=>'1') 
      ->pack( -side=>'top', -fill=>'both');

my $fileMenu = $menuFrame->Menubutton(
      -underline=>'0',
      -text=>"File (F10)",
      -tearoff=>0,
      -relief=>'raised',
      -menuitems=>[
          ['command'=>"Exit", -command=> sub {exit} ]])   
      ->pack( -side=>'left');


my $secondFrame = $mw1->Frame(-relief=>'ridge', -borderwidth=>'1') 
      ->pack( -side=>'top', -fill=>'both', -expand=>'y');

my $first_button = $secondFrame->Button( -text=>'another Widget',     
      -command=>\&another_widget) 
      ->pack( -side=>'left', -anchor=>'n');

my $second_button = $secondFrame->Button( -text=>'a third Widget',     
      -command=>\&third_widget) 
      ->pack( -side=>'left', -anchor=>'n');
MainLoop;
#--------------------------------------------------------
sub another_widget {

  if (Tk::Exists($third_widget)) { $third_widget->destroy(); }
unless (Tk::Exists($second_widget)){
  $second_widget = $secondFrame->Label(-text=>'This label is the second widget')->pack();
  }
}

sub third_widget {
if (Tk::Exists($second_widget)) { $second_widget->destroy(); }
unless (Tk::Exists($third_widget)){
  $third_widget = $secondFrame->Label(-text=>'This label is the third widget')->pack();
  }
}

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Hi!

Thank you for your answers, they were very helpful. I should get a book about Tk, but haven't
yet decided what to buy.

One additional question: Can one attach scrollbars to frame so that the frame (1 or more) contents
would be scrollable if it's going "out" from window? I've done something with canvas, but the scrollbar
sliders won't shrink (the scrolling works though). Scrollbars are quite confusing at the first glance
(compared to VB or BCB).

Thanks!

-comma
 
Use the "Scrolled" method it's the easiest.

e.g my$list = Scrolled('Listbox', -scrollbars => 'oeos');

THis creates a listbox widget with optional scrollbars on the left and at the bottom. If you want non-optional scrollers then omit the 'o'
 
Building a scrolled frame from scratch is a pain (pun ;-)). Instead, use the 'pane' widget.

Code:
#!/usr/local/bin/perl
use Tk;
use Tk::Pane;
use strict;

my $mw = new MainWindow;
$mw->Label(-text=>'Some Text in a Label above a scrolled pane')->pack(-side=>'top');

my $pane = $mw->Scrolled('Pane',
  -scrollbars=>'soe',
  -sticky=>'we',
  -width=>'600')->pack(-side=>'top', -anchor=>'w');

$pane->Button(-command=>\&insert_widgets,
  -text=>"Build Labels" )->pack(-side=>'left');  
$pane->Button(-command=> sub {exit},
  -text=>"Exit" )->pack(-side=>'right');  
MainLoop;

sub insert_widgets {
  my $i = 0;
  for ($i=1; $i < 25; $i++)
    {
    $pane->Label(-text=>&quot;Label number $i&quot;)->pack(-side=>'top');
    }  
}


If you are going to buy a book (I would), I'd suggest &quot;Learning Perl/Tk&quot; by Nancy Walsh published by O'Reilly (of course).

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Hi!

Thanks to all for good answers.

Tk seems very powerful and as a bonus it's multi-platform UI (no need for porting, e.g. Windows -> *nix) and Perl is also becoming more familiar (it's actually surprisingly powerful).

br, comma
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top