Re: Clean Shutdown

[email protected] (James Lynes)
Newsgroups perl.wxperl.users
Message-ID <CABPysehpoh2DvD8+XB8mJ0XXz76dLTDrNco_UBWHFcCzsHxpKQ@mail.gmail.com>
Steve:

The exit(0) seems to be causing a segmentation fault. Any Ideas?

Latest version of the code is attached.

Thanks,
James


On Thu, Jan 29, 2015 at 4:29 PM, James Lynes <[email protected]> wrote:

> Steve:
>
> I believe I looked at Glade a couple of years ago. Seems it kept crashing
> on me so I just went back to the old manual method.
>
> Thanks for the sample code.
>
> James
>
>
> On Thu, Jan 29, 2015 at 12:28 PM, Steve Cookson - gmail <
> [email protected]> wrote:
>
>> Hi James,
>>
>> I've replied to the list as well because I feel it needs a bit of loving!
>>
>> On 28/01/15 18:23, James Lynes wrote:
>>
>>> Hi Steve:
>>>
>>> How's your project rollout going?
>>>
>> Very kind of you to ask.  The launch went very well, since then we have
>> been dealing with the manufacturer and we are working to a delivery of the
>> first batch to Brazil by the end of March.  Our Brazilian salesman is
>> confident that he will sell all the first batch without too much difficulty.
>>
>>> What's the accepted method to shutdown an application?
>>>
>>> I can use die, but there must be a WX call that should be used. I don't
>>> want to leave a frame open on the screen that has to be closed manually.
>>>
>>> Example using die is attached. (Wrapper around a command line
>>> application)
>>>
>>
>> The short answer to your question is probably "exit(0);" called from an
>> exit button.
>>
>> However, the longer answer is did you ever see wxGlade?
>>
>> I don't know whether wxGlade was ever converted to 3.0.x, but it did work
>> on 2.8 and I thought 2.9.  It's a wonderful way to get started with all the
>> Wx paradigms.  I used it extensively at the beginning.
>>
>> I re-wrote the OnInit subroutine as below.  It uses Wx::BoxSizer and
>> Wx::Events, both of which will help your application.  I also used
>> Wx::FilePickerCtrl as a control to get the .pdf file.
>>
>> Have fun with it.
>>
>> Best regards
>>
>> Steve.
>>
>>
>> sub OnInit {
>>
>>     my $parent  = shift;
>>
>>
>>     my $frame = Wx::Frame->new(undef, wxID_ANY, "", wxDefaultPosition,
>> wxDefaultSize );
>>
>>     $parent->SetTopWindow($frame);
>>
>>     #Create controls
>>     # Fields
>>     my $pdfLabel=Wx::StaticText->new($frame, wxID_ANY,"PDF file",
>> wxDefaultPosition, wxDefaultSize);
>>     my $pdfFilePicker=Wx::FilePickerCtrl->new($frame, wxID_ANY, "",
>> "Pick a folder","*.pdf",wxDefaultPosition, [200,-1],
>> wxDIRP_DIR_MUST_EXIST|wxDIRP_CHANGE_DIR|wxDIRP_USE_TEXTCTRL);
>>     $pdfFilePicker->SetPath("/home/image/");
>>     my $cropLabel=Wx::StaticText->new($frame, wxID_ANY,"crop string",
>> wxDefaultPosition, wxDefaultSize);
>>     my $cropStringText=Wx::TextCtrl->new($frame, wxID_ANY,"crop string",
>> wxDefaultPosition, [200,-1]);
>>     # Buttons
>>     my $okButton = Wx::Button->new($frame, wxID_OK, "",
>> wxDefaultPosition, wxDefaultSize);
>>     my $cancelButton = Wx::Button->new($frame, wxID_CANCEL, "",
>> wxDefaultPosition, wxDefaultSize);
>>
>>     # Create sizers.
>>     my $verticalSizerFrame = Wx::BoxSizer->new(wxVERTICAL);
>>     $frame->SetSizer($verticalSizerFrame);
>>     my $verticalSizerControls = Wx::BoxSizer->new(wxVERTICAL);
>>     my $horizontalSizerButtons = Wx::BoxSizer->new(wxHORIZONTAL);
>>     # Lay 'em out.
>>     $verticalSizerFrame->Add($verticalSizerControls,0,0,0);
>>     $verticalSizerFrame->Add($horizontalSizerButtons,0,0,0);
>>
>>     $verticalSizerControls->Add($pdfLabel,0,0,0);
>>     $verticalSizerControls->Add($pdfFilePicker,0,0,0);
>>     $verticalSizerControls->Add($cropLabel,0,0,0);
>>     $verticalSizerControls->Add($cropStringText,0,0,0);
>>
>>     $horizontalSizerButtons->Add($okButton,0,0,0);
>>     $horizontalSizerButtons->Add($cancelButton,0,0,0);
>>
>>     # Event handlers
>>     Wx::Event::EVT_BUTTON($frame, $okButton, sub {
>>     my ($self, $event) = @_;
>>         exit (0);
>>     });
>>
>>     $verticalSizerFrame->Layout();
>>
>>     $frame->Show(1);
>>
>> }
>>
>>
>>> Thanks,
>>> James
>>>
>>
>>
>
stevecode.pl (application/x-perl, 4.2 KB)
#! /usr/bin/perl

# Name:			stevecode.pl
# Authors:		Steve Cookson & James M. Lynes, Jr.
# Created:		January 30, 2015
# Modified By:		James M. Lynes, Jr.
# Last Modified:	January 30, 2015
# Change Log:		1/30/2015 - Program Created
#			1/30/2015 - added events for filepicker change & textctrl change
#				  - added default filename and crop parameters
#				  - added spacers to sizers
#                                 - Modified some captions
#				  - Modified default path
#				  - Changed Cancel button to Exit(Quit) button
# Description:		Script to convert a pdf file to a png file	
#

package main;
use strict;
use warnings;
my $app = App->new();
$app->MainLoop;

package App;
use strict;
use warnings;
use base 'Wx::App';
sub OnInit {
    my $frame = Frame->new();
    $frame->Show(1);
}

package Frame;
use strict;
use warnings;
use Wx qw(:everything);
use base qw(Wx::Frame);
use Data::Dumper;

sub new {
    my ($class, $parent) = @_;

    my $frame = $class->SUPER::new($parent, -1, "PDF to PNG File Converter", wxDefaultPosition, wxDefaultSize);

    #Create controls
    # Fields
    my $pdfLabel=Wx::StaticText->new($frame, wxID_ANY,"Select a PDF file to Convert", wxDefaultPosition, wxDefaultSize);

    my $pdfFilePicker=Wx::FilePickerCtrl->new($frame, wxID_ANY, "", "Select the PDF File","*.pdf",
                          wxDefaultPosition, [400,-1], wxDIRP_DIR_MUST_EXIST|
                          wxDIRP_CHANGE_DIR|wxDIRP_USE_TEXTCTRL);
    $pdfFilePicker->SetPath("~/");
    $frame->{inpath} = "default.pdf";

    my $cropLabel=Wx::StaticText->new($frame, wxID_ANY,"Enter Image cropping Parameters", wxDefaultPosition, wxDefaultSize);
    my $cropStringText=Wx::TextCtrl->new($frame, wxID_ANY,"250x175+10+10", wxDefaultPosition, [200,-1]);
    $frame->{cropstring} = $cropStringText->GetValue;

    # Buttons
    my $okButton = Wx::Button->new($frame, wxID_OK, "", wxDefaultPosition, wxDefaultSize);
    my $exitButton = Wx::Button->new($frame, wxID_EXIT, "", wxDefaultPosition, wxDefaultSize);

    # Create sizers.
    my $verticalSizerFrame = Wx::BoxSizer->new(wxVERTICAL);
    $frame->SetSizer($verticalSizerFrame);
    my $verticalSizerControls = Wx::BoxSizer->new(wxVERTICAL);
    my $horizontalSizerButtons = Wx::BoxSizer->new(wxHORIZONTAL);

    # Lay 'em out.
    $verticalSizerFrame->Add($verticalSizerControls,0,0,0);
    $verticalSizerFrame->Add($horizontalSizerButtons,0,0,0);

    $verticalSizerControls->AddSpacer(15);              # Spacer
    $verticalSizerControls->Add($pdfLabel,0,0,0);
    $verticalSizerControls->Add($pdfFilePicker,0,0,0);
    $verticalSizerControls->AddSpacer(15); 		# Spacer 
    $verticalSizerControls->Add($cropLabel,0,0,0);
    $verticalSizerControls->Add($cropStringText,0,0,0);
    $verticalSizerControls->AddSpacer(15); 		# Spacer

    $horizontalSizerButtons->Add($okButton,0,0,0);
    $horizontalSizerButtons->Add($exitButton,0,0,0);

    # Event handlers
    Wx::Event::EVT_BUTTON($frame, $okButton, sub {
                          my ($self, $event) = @_;
                          print "\nOK Button\n";
			  # Convert the pdf file to png using
			  # "convert" command line command via backticks
                          my $inpath = $self->{inpath};
                          my $crop = $self->{cropstring};
                          my $convertcommand = "convert $inpath -crop $crop $inpath.png";
                          print "\n\n";
                          print $convertcommand;
                          print "\n\n";
                          });

    Wx::Event::EVT_BUTTON($frame, $exitButton, sub {
                          my ($self, $event) = @_; 
                          print "\nEXIT Button\n";
                          exit (0);			# Causing Sgementation Fault(core dumped)
                          });

    Wx::Event::EVT_FILEPICKER_CHANGED($frame, $pdfFilePicker, sub {
                          my ($self, $event) = @_;
                          $self->{inpath} = $pdfFilePicker->GetPath;
                          });

    Wx::Event::EVT_TEXT($frame, $cropStringText, sub {
                          my ($self, $event) = @_;
                          $self->{cropstring} = $cropStringText->GetValue;
                          });

    $verticalSizerFrame->Layout();

    return $frame;

}
1;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.