paulehr has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks

I am trying to learn Win32::GUI for a small personal project i am working on. I eventually would like to do is display several lines of output into the main window, have a button that opens up browser when clicked, and sit in the systray when minimized. I have the last two things figured out and working using the examples on the project's CPAN page. However I am getting stuck with how to display multiple lines of text in the main window.

I have tried to do the following using Addlabel:

#!/usr/bin/perl -w use strict; use Win32::GUI; my $mw = Win32::GUI::Window->new ( -name => 'Main', -width => 300, -height => 100, -text => 'bbtrack', ); my $label1 = $mw->AddLabel( -text => "hello"); my $label2 = $mw->AddLabel( -text => "hello2"); $label1->show(); $label2->show(); $mw->Show(); Win32::GUI::Dialog();

This however doesnt work and i end up with the following error

Can't locate auto/Win32/GUI/Label/show.al in @INC (@INC contains: C:/P +erl/lib C: /Perl/site/lib .) at guitest.pl line 15

Am I perhaps going about this the wrong way? or/and there is a better way of doing what i am trying to do?

thanks for the help in advance

Replies are listed 'Best First'.
Re: adding multiple labels with win32::GUI
by rcseege (Pilgrim) on Sep 09, 2006 at 20:56 UTC

    Update: For more information on dealing with Label positioning read below. After rereading your post, it sounds like you might be better off using a TextField, ListView, or Listbox control as BrowserUk suggested.

    First, it should be Show instead of show, and second, you can drop it from the Label controls -- it's unnecessary.

    This leaves you with the following:

    use strict; use Win32::GUI; my $mw = Win32::GUI::Window->new ( -name => 'Main', -width => 300, -height => 100, -text => 'bbtrack', ); my $label1 = $mw->AddLabel( -text => "hello"); my $label2 = $mw->AddLabel( -text => "hello2"); $mw->Show(); Win32::GUI::Dialog();

    What is going on is you are laying one Label directly on top of the other one so it looks as though only one is created. To verify this, change the text of the first Label to "hello world". Now, the overlay should be easy to see.

    Try specifying the position of the second Label by using one or more of the following options: -left, -top, or -pos. Here are a few examples:

    my $label2 = $mw->AddLabel( -text => "hello2", -left => 30);

    or even:

    my $label2 = $mw->AddLabel( -text => "hello2", -top => 15);

    Of course, this can be problematic because of fonts so you could use the Height and Width methods:

    my $label2 = $mw->AddLabel( -text => "hello2", -left => ($label1->Width + 2)); ## OR my $label2 = $mw->AddLabel( -text => "hello2", -top => ($label1->Height + 2));

    You might also take a look at the GridLayout control. Here's one example:

    use Win32::GUI; use Win32::GUI::GridLayout; my $mw = Win32::GUI::Window->new ( -name => 'Main', -width => 300, -height => 100, -text => 'bbtrack', ); my $grid = apply Win32::GUI::GridLayout($mw, 4, 4, 0, 0); for my $col (1 .. 4) { for my $row (1 .. 4) { my $lab = $mw->AddLabel( -name => "MainLabel$col$row", -text => "C: $col, R: $row"); $grid->add($lab, $col, $row); } } $grid->recalc; $mw->Show(); Win32::GUI::Dialog(); sub Main_Resize { $grid->recalc(); }
    Rob
Re: adding multiple labels with win32::GUI
by BrowserUk (Patriarch) on Sep 09, 2006 at 20:31 UTC

    Try $label1->Show(); rather than $label1->show();. You even have an example: $mw->Show(); 2 lines away :)

    Also, if you add the -wrap=1 option to your label, and make it big enough for multiple lines, it will avoid the need for multiple labels.

    However, if you want to control where new lines start yourself, then you'd probably better of with a listbox rather than a label.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: adding multiple labels with win32::GUI
by paulehr (Sexton) on Sep 09, 2006 at 22:03 UTC

    Thanks guys, I can't believe i missed that. Sometimes its good to have a second set of eyes to take a look :)

    I'll play around some more with your recommendations and see what i come up with, thanks!