Questions to perl/Tk gurus (was: TK Problems with Menu/MenuButton) 
Author Message
 Questions to perl/Tk gurus (was: TK Problems with Menu/MenuButton)

=pod
Hi Steve

I experimented and I solved both your problems, but I'm confused. Can a
perl/Tk guru help?
I have perl ActiveState build 514 and the perl/Tk recently installed from
ActiveState via PPM.

I confirmed your first problem (menu from 'Advanced Perl Programming', page
 237) ...
"No -label at C:\Perl\site\lib/Tk/Widget.pm line 240."

The fix is: precede each of 5 occurencies of label by a dash, e.g. (-label
=> 'Find').

In the same script, found in file menu.pl from examples.tar.gz, (from
O'Reilly website -
look for Sriram Srinivasan code examples), all option names both have a
dash and are quoted, like this ('-label' => 'Find').

Now a few questions to perl/Tk gurus:
Why did not other Tk options need the dash?
Does 'label' clash with some other use of this identifier?
Is it a perl/Tk bug?
What IS the recommended practice : dash/no dash, and quote/no quote?

With your second example (from the Perl Cookbook) I had a different
problem: the program went into an infinite loop.
Stepping under ptkdb showed it recursing forever into sub AUTOLOAD, in
Tk/Widget.pm.

The fix is to replace MenuButton by Menubutton, in line
my $file_menu = $menubar->MenuButton(-text => "File", #...

Now another question to perl/Tk gurus: why is this not detected as error?
Replacing MenuButton by MenuZipper produces a clear diagnostic:

C:\Perl\site\lib .) at C:\Perl\site\lib/Tk/Widget.pm line 311.

Steve, below is your code with my mods. I folded both examples into one,
and they work for me.
I also added use strict, but that did not help ;-(

Best regards
Rudi Farkas
PS for the ptk list people: Steve posted his problem to

=cut

use strict;

use Tk;

#
# 1st example
#

my $top = MainWindow->new();
my $menu_bar = $top->Frame()->pack(side => 'top');
my $search_mb = $menu_bar->Menubutton(text => 'Search',
                                   relief => 'raised',
                                   borderwidth => 2,
                                   )->pack(side => 'left',
                                           padx => 0);
#$search_mb->command(label => 'Find',                             ### fails
$search_mb->command(-label => 'Find',                             ### ok
                    accelerator => 'Meta-F',
                    underline => 0,
                    command => sub {print "find\n" }
                   );
#$search_mb->command(label => 'Find Again',                       ### fails
$search_mb->command(-label => 'Find Again',                       ### ok
                    accelerator => 'Meta-A',
                    underline => 5,
                    command => sub {print "find again\n" }
                   );
$search_mb->separator();
my $match_type = 'regexp';
my $case_type = 1;
#$search_mb->radiobutton(label => 'Regexp match',            ### fails
$search_mb->radiobutton(-label => 'Regexp match',            ### ok
                        value => 'regexp',
                        variable => \$match_type);
#$search_mb->radiobutton(label => 'Exact match',             ### fails
$search_mb->radiobutton(-label => 'Exact match',             ### ok
                        value => 'exact',
                        variable => \$match_type);
$search_mb->separator();
#$search_mb->checkbutton(label => 'Ignore case?',            ### fails
$search_mb->checkbutton(-label => 'Ignore case?',            ### ok
                        variable => \$case_type);

#
# 2nd example
#

my $menubar = $top->Frame(-relief => "raised",
                        -borderwidth => 2)
                        ->pack (-anchor => "nw",
                                -fill => "x");
#my $file_menu = $menubar->MenuButton(-text => "File",       ### fails
#my $file_menu = $menubar->MenuZipper(-text => "File",       ### fails
my $file_menu = $menubar->Menubutton(-text => "File",        ### ok
                                  -underline => 1)
                        ->pack (-side => "left");
$file_menu->command(-label => "Print", -command => sub {print "find
again\n" } );

MainLoop();

==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Fri, 02 Nov 2001 03:00:00 GMT  
 Questions to perl/Tk gurus (was: TK Problems with Menu/MenuButton)

Quote:

> =pod
> Hi Steve

> I experimented and I solved both your problems, but I'm confused. Can a
> perl/Tk guru help?
> I have perl ActiveState build 514 and the perl/Tk recently installed from
> ActiveState via PPM.

> I confirmed your first problem (menu from 'Advanced Perl Programming', page
>  237) ...
> "No -label at C:\Perl\site\lib/Tk/Widget.pm line 240."

> The fix is: precede each of 5 occurencies of label by a dash, e.g. (-label
> => 'Find').

Correct fix.   All Perl/Tk options should always have a leading dash.  Always.
No exceptions, forever, until Nick says so (-:  

I still haven't seen errata for this on the O'Reilly page... although "unconfirmed"
notes mention this ala HList.

It's really very simple - Tcl/Tk option processing parallels U*x command line
processing; thus, we have:

        command [-arg value]

with white space separating "words", where a word might be a quote-delimited string.

Perl/Tk inherits this, mostly.

Quote:

> In the same script, found in file menu.pl from examples.tar.gz, (from
> O'Reilly website -
> look for Sriram Srinivasan code examples), all option names both have a
> dash and are quoted, like this ('-label' => 'Find').

It almost never hurts to quote the LHS of =>.  For the most part it's unnecessary,
unless the word conflicts with Perl builtin keyword.  For instance,

        -pop => 'frog'

must be written as:

        '-pop' => 'frog'

Well, with -w anyway.

Quote:

> Now a few questions to perl/Tk gurus:
> Why did not other Tk options need the dash?

They all need a dash!

Quote:
> Does 'label' clash with some other use of this identifier?

Not normally, but it might depend on the code.

Quote:
> Is it a perl/Tk bug?

No.

Quote:
> What IS the recommended practice : dash/no dash, and quote/no quote?

1)  Use -w.
2)  use strict;
3)  -arg => value

        a. always use a leading dash for the argument.
        b. if -w complains because it's not clear how to resolve something,
           then quote "-arg".  This rarely happens.

Quote:

> With your second example (from the Perl Cookbook) I had a different
> problem: the program went into an infinite loop.
> Stepping under ptkdb showed it recursing forever into sub AUTOLOAD, in
> Tk/Widget.pm.

> The fix is to replace MenuButton by Menubutton, in line
> my $file_menu = $menubar->MenuButton(-text => "File", #...

Yes, the correct widget class/name is "Menubutton".  I haven't seen/tried the
example...  can you devise a small testcase?

Quote:

> Now another question to perl/Tk gurus: why is this not detected as error?
> Replacing MenuButton by MenuZipper produces a clear diagnostic:

> C:\Perl\site\lib .) at C:\Perl\site\lib/Tk/Widget.pm line 311.

> Steve, below is your code with my mods. I folded both examples into one,

I assume I'm not the correct Steve, but I'll look into it (-:

- Show quoted text -

Quote:
> and they work for me.
> I also added use strict, but that did not help ;-(

> Best regards
> Rudi Farkas
> PS for the ptk list people: Steve posted his problem to

> =cut

> use strict;

> use Tk;

> #
> # 1st example
> #

> my $top = MainWindow->new();
> my $menu_bar = $top->Frame()->pack(side => 'top');
> my $search_mb = $menu_bar->Menubutton(text => 'Search',
>                                    relief => 'raised',
>                                    borderwidth => 2,
>                                    )->pack(side => 'left',
>                                            padx => 0);
> #$search_mb->command(label => 'Find',                             ### fails
> $search_mb->command(-label => 'Find',                             ### ok
>                     accelerator => 'Meta-F',
>                     underline => 0,
>                     command => sub {print "find\n" }
>                    );
> #$search_mb->command(label => 'Find Again',                       ### fails
> $search_mb->command(-label => 'Find Again',                       ### ok
>                     accelerator => 'Meta-A',
>                     underline => 5,
>                     command => sub {print "find again\n" }
>                    );
> $search_mb->separator();
> my $match_type = 'regexp';
> my $case_type = 1;
> #$search_mb->radiobutton(label => 'Regexp match',            ### fails
> $search_mb->radiobutton(-label => 'Regexp match',            ### ok
>                         value => 'regexp',
>                         variable => \$match_type);
> #$search_mb->radiobutton(label => 'Exact match',             ### fails
> $search_mb->radiobutton(-label => 'Exact match',             ### ok
>                         value => 'exact',
>                         variable => \$match_type);
> $search_mb->separator();
> #$search_mb->checkbutton(label => 'Ignore case?',            ### fails
> $search_mb->checkbutton(-label => 'Ignore case?',            ### ok
>                         variable => \$case_type);

> #
> # 2nd example
> #

> my $menubar = $top->Frame(-relief => "raised",
>                         -borderwidth => 2)
>                         ->pack (-anchor => "nw",
>                                 -fill => "x");
> #my $file_menu = $menubar->MenuButton(-text => "File",       ### fails
> #my $file_menu = $menubar->MenuZipper(-text => "File",       ### fails
> my $file_menu = $menubar->Menubutton(-text => "File",        ### ok
>                                   -underline => 1)
>                         ->pack (-side => "left");
> $file_menu->command(-label => "Print", -command => sub {print "find
> again\n" } );

> MainLoop();

> ==========================================================================
> This message was posted through the Stanford campus mailing list
> server.  If you wish to unsubscribe from this mailing list, send the


==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Fri, 02 Nov 2001 03:00:00 GMT  
 Questions to perl/Tk gurus (was: TK Problems with Menu/MenuButton)
Many thanks... sorry to have caused such a stir... I have typed in some
source code wrong and I don't know diddly about tk style which Nick
suggested that I try what he termed Native style... I am still working
at fully understanding all the wonderful things that can be done with
TK...

My errors come more from relying on books as gospel and not being sure
how exactly to debug the situation even though I have the activestate
de{*filter*} which is great for command line stuff but haven't seen how you
connect up the graphical stuff to it and see what the heck is going on
in there...

Many thanks to the replies and I'll be more careful about checking out
everything so that I make sure I understand what I am asking...

Thanks,

Stephen de Vries

==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Sat, 03 Nov 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Questions to perl/Tk gurus (was: TK Problems with Menu

2. BETA TEST: Tk::Tie::MenuHash - Ties a Tk::Menubutton widget to a hash, kinda

3. BETA TEST: Tk::Tie::MenuHash - Ties a Tk::Menubutton widget to a hash, kinda

4. Tk::FcyEntry doesn't work with Tk::Menu

5. Question on Tk menu backgrounds

6. Newbie question about cascaded menus in TK

7. what Tk Tk/perl Tkperl perl/Tk?

8. Perl/Tk Packages (Was: RE: (More) Problems Installing Perl/Tk)

9. Tk::Pretty problem with menu's

10. Newbie questions: version of tk in Perl/tk?

11. Perl/Tk and Fonts (Tk::Fonts) Question

12. Error in Menubutton::Post in Tk-b10?

 

 
Powered by phpBB® Forum Software