Hello, I was trying to create some windows using perl and the WX lib. I've found the answer on how to get it working but i have no clue why. this is code I found online as example, this opens a window with a title bar:
use strict; use warnings; use Wx; use Wx qw(wxOK wxCENTRE); package WxPerlComExample; use base qw(Wx::App); sub OnInit { my $self = shift; my $frame = WxPerlComExampleFrame->new(undef, -1, "WxPerl Example" +); $frame->Show(1); $self->SetTopWindow($frame); return 1; } package WxPerlComExampleFrame; use base qw(Wx::Frame); use Wx qw( wxDefaultPosition wxDefaultSize wxDefaultPosition wxDefaultSize wx +ID_EXIT ); use Wx::Event qw(EVT_MENU); our @id = (0 .. 100); # IDs array sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); # Create menus my $firstmenu = Wx::Menu->new(); $firstmenu->Append($id[0], "Normal Item"); $firstmenu->AppendCheckItem($id[1], "Check Item"); $firstmenu->AppendSeparator(); $firstmenu->AppendRadioItem($id[2], "Radio Item"); my $secmenu = Wx::Menu->new(); $secmenu->Append(wxID_EXIT, "Exit\tCtrl+X"); # Create menu bar my $menubar = Wx::MenuBar->new(); $menubar->Append($firstmenu, "First Menu"); $menubar->Append($secmenu, "Exit Menu"); # Attach menubar to the window $self->SetMenuBar($menubar); $self->SetAutoLayout(1); # Handle events only for Exit and Normal item EVT_MENU( $self, $id[0], \&ShowDialog ); EVT_MENU( $self, wxID_EXIT, sub {$_[0]->Close(1)} ); return $self; } sub ShowDialog { my($self, $event) = @_; Wx::MessageBox( "This is a dialog", "Wx::MessageBox example", #wxOK|wxCENTRE, $self ); } package main; my($app) = WxPerlComExample->new(); $app->MainLoop();
now i also found some code which would open a window without a title bar but with some buttons. I wanted to integrate both.
use strict; use warnings; use Wx; use Wx qw(wxOK wxCENTRE); use wxPerl::Constructors; package MyApp; use base 'Wx::App'; use Win32::GUI (); sub OnInit { my $self = shift; my $frame = MyAppFrame->new(undef, -1, 'A wxPerl Application'); # my $frame = wxPerl::Frame->new(undef, 'A wxPerl Application'); $frame->SetMinSize([120,80]); my $menubar = Wx::MenuBar->new(); my $sizer = Wx::BoxSizer->new(&Wx::wxVERTICAL); my $button = wxPerl::Button->new($frame, 'Click Me'); $sizer->Add($button, 2, &Wx::wxEXPAND); my $button3 = wxPerl::Button->new($frame, 'Maybe Click'); $sizer->Add($button3, 2, &Wx::wxEXPAND); my $button4 = wxPerl::Button->new($frame, 'select a file'); $sizer->Add($button4, 2, &Wx::wxEXPAND); my $button2 = wxPerl::Button->new($frame, 'Close'); $sizer->Add($button2, 2, &Wx::wxEXPAND); Wx::Event::EVT_BUTTON($button, -1, sub { my ($b, $evt) = @_; $b->SetLabel('Clicked'); $b->Disable; }); Wx::Event::EVT_BUTTON($button2, -1, sub { &Wx::wxTheApp->ExitMainLoop; }); my $i=0; Wx::Event::EVT_BUTTON($button3, -1, sub { my ($b, $evt) = @_; if ($i == 0){ $b->SetLabel('Maybe'); $i++; } else { $b->SetLabel('Maybe not'); $i--; } }); Wx::Event::EVT_BUTTON($button4, -1, sub { my ($b, $evt) = @_; my $file = Win32::GUI::GetOpenFileName(-filemustexist => 1,); if (defined $file) { $b->SetLabel("input: $file"); } else { $b->SetLabel('unknown'); } }); $frame->SetSizer($sizer); $frame->Show(1); $self->SetTopWindow($frame); return 1; } package MyAppFrame; use base qw(Wx::Frame); use Wx qw( wxDefaultPosition wxDefaultSize wxDefaultPosition wxDefaultSize wx +ID_EXIT ); use Wx::Event qw(EVT_MENU); our @id = (0 .. 100); sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); # Create menus my $firstmenu = Wx::Menu->new(); $firstmenu->Append($id[0], "Normal Item"); $firstmenu->AppendCheckItem($id[1], "Check Item"); $firstmenu->AppendSeparator(); $firstmenu->AppendRadioItem($id[2], "Radio Item"); my $secmenu = Wx::Menu->new(); $secmenu->Append(wxID_EXIT, "Exit\tCtrl+X"); # Create menu bar my $menubar = Wx::MenuBar->new(); $menubar->Append($firstmenu, "First Menu"); $menubar->Append($secmenu, "Exit Menu"); # Attach menubar to the window $self->SetMenuBar($menubar); $self->SetAutoLayout(1); # Handle events only for Exit and Normal item EVT_MENU( $self, $id[0], \&ShowDialog ); EVT_MENU( $self, wxID_EXIT, sub {$_[0]->Close(1)} ); return $self; } sub ShowDialog { my($self, $event) = @_; Wx::MessageBox( "This is a dialog", "Wx::MessageBox example", #wxOK|wxCENTRE, $self ); } # create the application object, this will call OnInit # before the constructor returns. package main; my($app) = MyApp->new(); # process GUI events from the application this function # will not return until the last frame is closed $app->MainLoop();
It took me a while to figure out this was my error:
my $frame = MyAppFrame->new(undef, -1, 'A wxPerl Application'); # my $frame = wxPerl::Frame->new(undef, 'A wxPerl Application');
with the first line all works fine, with the 2nd the menubar will not appear, just a window with buttons... What is the difference??? Could someone try to explain me? wkr, Luk

In reply to question about creating windows using WX module by Luk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.