Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Need help doing simple tasks in WxPerl / Wxglade

by netnem (Initiate)
on Jun 06, 2014 at 03:14 UTC ( [id://1088975]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to migrate from using Win32::Gui (as TheGuiLoft is no longer supported) to WxGlade/WxPerl as I can create much nicer looking GUIs with a builder. However, I can't seem to figure out the very basics of just running regular Perl code after clicking a button in WxPerl.

My question is, from the below code, how do I run a subroutine outside of WxPerl with the variables pushed from the GUI into the script? The only programming language I know is Perl, and Win32::GUI comes naturally to me from just reading CPAN documentation, and Wx is really giving me a hard time.

Things I want the GUI to do:

1.) How to print "Hello World" (from the button eventhandler); it doesn't actually print to console until I hit the X button and close the GUI.

Win32::GUI equiv:
$main->AddButton( -text => 'example_button', onClick => sub { print +"Hello World";

2.) I need to be able to print the value of the radio button or checkbox and run a subroutine based on if it's set to 0 or 1 after I click the button from above.

 (if ($radiobutton=1) {sub { code here};  )

or win32 equiv:

$main->checkbox->GetCheck;

3.) Capture the input value from the user and pass to console as variables win32 equiv:

my @list = $input->multiline_textinput->Text();

(Below is majority output from WxGlade) It consists of 1 radio, 1 textbox, 1 button, and 1 checkbox.

use Wx qw[:everything]; use base qw(Wx::Frame); use strict; use Wx::Locale gettext => '_T'; sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_ +; $parent = undef unless defined $parent; $id = -1 unless defined $id; $title = "" unless defined $title; $pos = wxDefaultPosition unless defined $pos; $size = wxDefaultSize unless defined $size; $name = "" unless defined $name; # begin wxGlade: MyFrame1::new $style = wxDEFAULT_FRAME_STYLE unless defined $style; $self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $sty +le, $name ); $self->{panel_1} = Wx::Panel->new($self, wxID_ANY, wxDefaultPositi +on, wxDefaultSize, ); $self->{radio_btn_1} = Wx::RadioButton->new($self->{panel_1}, wxID +_ANY, _T("radio_btn_1"), wxDefaultPosition, wxDefaultSize, ); $self->{checkbox_1} = Wx::CheckBox->new($self->{panel_1}, wxID_ANY +, _T("checkbox_1"), wxDefaultPosition, wxDefaultSize, ); $self->{combo_box_1} = Wx::ComboBox->new($self->{panel_1}, wxID_AN +Y, "", wxDefaultPosition, wxDefaultSize, [], wxCB_DROPDOWN); $self->{button_1} = Wx::Button->new($self->{panel_1}, wxID_ANY, _T +("button_1")); $self->__set_properties(); $self->__do_layout(); Wx::Event::EVT_BUTTON($self, $self->{button_1}->GetId, \&mycode); # end wxGlade return $self; } sub __set_properties { my $self = shift; # begin wxGlade: MyFrame1::__set_properties $self->SetTitle(_T("frame_2")); $self->{combo_box_1}->SetSelection(-1); # end wxGlade } sub __do_layout { my $self = shift; # begin wxGlade: MyFrame1::__do_layout $self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL); $self->{sizer_2} = Wx::BoxSizer->new(wxHORIZONTAL); $self->{sizer_2}->Add($self->{radio_btn_1}, 0, 0, 0); $self->{sizer_2}->Add($self->{checkbox_1}, 0, 0, 0); $self->{sizer_2}->Add($self->{combo_box_1}, 0, 0, 0); $self->{sizer_2}->Add($self->{button_1}, 0, 0, 0); $self->{panel_1}->SetSizer($self->{sizer_2}); $self->{sizer_1}->Add($self->{panel_1}, 1, wxEXPAND, 0); $self->SetSizer($self->{sizer_1}); $self->{sizer_1}->Fit($self); $self->Layout(); # end wxGlade } sub mycode { my ($self, $event) = @_; # wxGlade: MyFrame1::mycode <event_handler> print "\n\n\nHello World\n\n\n"; warn "Event handler (mycode) not implemented"; $event->Skip; # end wxGlade } # end of class MyFrame1 1; 1; package main; unless(caller){ my $local = Wx::Locale->new("English"); # replace with ?? $local->AddCatalog("app"); # replace with the appropriate catalogn +ame local *Wx::App::OnInit = sub{1}; my $app = Wx::App->new(); Wx::InitAllImageHandlers(); my $frame_1 = MyFrame1->new(); $app->SetTopWindow($frame_1); $frame_1->Show(1); $app->MainLoop(); }

I feel as if I'm so close to being able to create extremely pleasant looking (cross platform) GUI applications, but can't seem to get over the last hump of the basic things. Any help is appreciated. :)

Replies are listed 'Best First'.
Re: Need help doing simple tasks in WxPerl / Wxglade
by Anonymous Monk on Jun 06, 2014 at 04:07 UTC

    My question is, from the below code, how do I run a subroutine outside of WxPerl with the variables pushed from the GUI into the script?

    What does that even mean?

    1.) How to print "Hello World" (from the button eventhandler); it doesn't actually print to console until I hit the X button and close the GUI.

    You're Suffering from Buffering?, STDOUT->autoflush(1); ... print "...\n\n\n\n\n\n\n";

    2.) I need to be able to print the value of the radio button or checkbox and run a subroutine based on if it's set to 0 or 1 after I click the button from above. 3.) Capture the input value from the user and pass to console as variables win32 equiv:

    Try  warn "@_\n"; from your event handler

    Then you can use

    $self->{checkbox_1}->GetValue $self->{combo_box_1}->GetValue $self->{radio_btn_1}->GetValue

    You've got typos ... modern practice

    my $app = Wx::SimpleApp->new; my $frame_1 = MyFrame1->new(); $app->SetTopWindow($frame_1); $frame_1->Show(1); $app->MainLoop();

    my wxWidgets / wxPerl / wxGlade tutorial

      Thanks for this response. I guess I was assuming that WxPerl was intercepting my Perl commands and not running them, and in my head I was segregating the GUI from the program, and not understanding why the program wasn't running until the GUI closed. It actually looks like it might be a buffering issue with auto flush. I've tried to printf in the event handler, but it seemed to do the same

      I think this post will be enough to get over the initial hump. Thank you so much!

      Update: Yes!!!!!! The above post has just opened up so much for me in regards to working with Wx.

      Adding the following line gives output immediately to console log and finally "makes sense" to me with what Wx is actually doing.

      select((select(STDOUT), $|=1)[0]);

      Thank you so much. I should be able to port my program from Win32 to Wx now.

Re: Need help doing simple tasks in WxPerl / Wxglade
by scorpio17 (Canon) on Jun 06, 2014 at 13:42 UTC
    Did you look in the Tutorials section of this site?
    Under "User Interfaces," there's a whole section devoted to "WxPerl".
    Maybe looking at some examples will help?

      Yes, I have. I've read through so many examples and tutorials which is why it became increasingly frustrating. Win32::Gui just made sense to me even when first looking at it. WxPerl is very greek-like to me. I've probably spent 6-8 man hours reading through documentation and trying different things, and I think the above post about the buffer auto-flush will help tremendously.

      I'm going to give it another shot this morning, as I think I might have enough information now to get over that initial steep learning curve for Wx.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1088975]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found