If you cancel the dialog, the $colour variable is undefined. You then try to set the -background and -activebackground to an undefined value. CRASH!!
You need to check that $colour is defined before using it.
use strict;
use warnings;
use Tk;
my $cfg;
my $font_colour;
my $wd = MainWindow->new();
$wd->Label(
-text => 'Color',
-justify => 'right',
)->grid(
-column => 0,
-row => 4,
-pady => 20,
-padx => 20,
-sticky => 'e'
);
my $colour_button = $wd->Button(
-width => 6,
-command => sub { $font_colour = colour_picker() },
)->grid(
-column => 2,
-row => 4,
-pady => 20,
-padx => 20,
-sticky => 'e'
);
if ($font_colour) {
$colour_button->configure( -background => $font_colour, -activebackg
+round => $font_colour );
}
MainLoop;
sub colour_picker {
my $colour = $wd->chooseColor( -title => 'Color Picker', -initialcol
+or => '#000000' );
$colour ? print "Colour chosen: " . $colour . "\n" : return;
$colour_button->configure( -background => $colour, -activebackground
+ => $colour );
return $colour;
}