#!/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 => $height); $btn->configure(-command => sub { $psub->() }); $btn->pack(); return $btn; }