Hi alw,

As the Anonymous Monk said, there isn't a way of cloning a widget.  However, you can achieve similar functionality by just creating a subroutine to handle the details of the widget you wish to "duplicate".

For example, here's a short program that creates two nearly identical buttons, each on a separate page of a 2-page NoteBook:

#!/usr/bin/perl -w use strict; use warnings; use Tk; use Tk::NoteBook; # Main program my $mw = new MainWindow(-title => 'Duplicated widget example'); my $nb = $mw->NoteBook()->pack(); my $pg1 = $nb->add("Page 1", -label => "Page 1"); my $pg2 = $nb->add("Page 2", -label => "Page 2"); my $bu1 = add_button($pg1, "Press Me", "I'm a button on Page 1"); my $bu2 = add_button($pg2, "Press Me", "I'm a button on Page 2"); MainLoop; # Subroutines sub add_button { my ($page, $label, $output) = @_; # These attributes will be common to the button my $bg = 'hotpink'; my $width = 20; my $height = 5; my $font = 'Helvetica 20'; # What happens when the button is pressed? my $psub = sub { print "$output\n"; }; # Create the Button widget and pack it my $btn = $page->Button(-bg => $bg, -text => $label); $btn->configure(-font => $font, -width => $width, -height => $heig +ht); $btn->configure(-command => sub { $psub->() }); $btn->pack(); return $btn; }

The subroutine add_button handles the details of the button you wish to duplicate.  When you call add_button, it adds a button to the specified page of the NoteBook ($1) with the given label ($2) and output text ($3).

Any other attribute of the button (ie. anything not passed as a subroutine argument) will be the same for each button created.  In the example I've given you, the color, font, and size (height and width) are all constant for any button created with the subroutine.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Tk widget in 2 toplevels by liverpole
in thread Tk widget in 2 toplevels by alw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.