package Tk::Confirmation; use strict; use warnings; use Carp; use Tk; use Tie::RefHash; use base 'Tk::Toplevel'; my %object; tie %object, 'Tie::RefHash'; Construct Tk::Widget 'Confirmation'; my % btn_cfg = ( -width => 6, -disabledforeground => '#777777', -highlightcolor => '#0000ff', -highlightthickness => 2, ); sub Populate { my @sub = map { delete $_[1]{$_} || sub {} } qw( -yes -no ); my $label = delete $_[1]{-label} || 'Are you sure?'; my $s = $_[0]; $s->SUPER::Populate( $_[1] ); my $yes = sub { $s->withdraw; $sub[0]->() }; my $no = sub { $s->withdraw; $sub[1]->() }; $s->protocol( 'WM_DELETE_WINDOW', $no ); $s->Label( -text => $label )->pack; $object{ $s }{ yes } = $s->Button( -text => 'Yes', -command => $yes, %btn_cfg )->pack( -side => 'left' ); $object{ $s }{ no } = $s->Button( -text => 'No', -command => $no, %btn_cfg )->pack; $object{ $s }{ no }->focus; } 1;