http://qs321.pair.com?node_id=617258

jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

It was time to add some colour choices to various things in my monster Perl/Tk application. So I added a simple little method using chooseColor, but alas it was a dismal failure. Oh, I could choose a colour alright, but alas, if you wanted to close the colour picker window ir click on the 'Cancel' button, it crashes the application.

Here is a minimal test case that I can make crash on my machine.

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' ); print "Colour chosen: " . $colour . "\n" if $colour; $colour_button->configure( -background => $colour, -activebackground + => $colour ); return $colour; }
In the event viewer the error is always:
Faulting application perl.exe, version 5.8.8.820, faulting module msvc +rt.dll, version 7.0.2600.2180, fault address 0x000378c0.
I am using Perl 5.8.8.820 Activestate build, Windows XP-SP2.

I am not knowledgable enough to know how to troubleshoot this any further myself, so any assistance would be appreciated.

jdtoronto

Replies are listed 'Best First'.
Re: Problem with Tk::chooseColor on Win32
by thundergnat (Deacon) on May 24, 2007 at 17:45 UTC

    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; }
      ...just curious : Is there an similar command to bring out the font-selection-window on win32 from perl/Tk also?

        Not that I am aware of. It isn't too hard to fake it though... and you could probably access it through Win32::API with a little tinkering.

Re: Problem with Tk::chooseColor on Win32
by ldln (Pilgrim) on May 24, 2007 at 16:28 UTC
    Works fine here.

    perl5.8.8 (build 817), Tk804.027, WinXP SP2.

    UPDATE
    Sorry, didn't read text closely enough. Yeah, ugly bug here also if "Cancel" button is pressed. Sorry.

    UPDATE
    Ok, simple fix. Just make sure you don't try to assign "undef color", if user press cancel. Maybe add simple if test:

    ... if ($colour) { $colour_button->configure( -background => $colour, -activeback +ground => $colour ); } ...
      Thanks for the update ldln, yeah, it is kinda ugly! Now all I have to do is figure out how get the nasty little critter back on his feet instead of lying around on his back!

      jdtoronto

Re: Problem with Tk::chooseColor on Win32
by zentara (Archbishop) on May 24, 2007 at 17:14 UTC
      Thanks, ldln & zentara and also Albannach who responded in the CB. I know it works fine on Linux! But Linux doesn't use the Win32 native colour chooser, nor does it have msvcrt.dll! I have tried it on 3 machines now, two are XP-XP2, the other is Win2k - all are very up to date. I have some others I can try, but ti is looking pretty consistent. My tech support guy has the same failure on his machine too.

      jdtoronto