Luk has asked for the wisdom of the Perl Monks concerning the following question:
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); 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();
It took me a while to figure out this was my error: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();
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, Lukmy $frame = MyAppFrame->new(undef, -1, 'A wxPerl Application'); # my $frame = wxPerl::Frame->new(undef, 'A wxPerl Application');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: question about creating windows using WX module
by choroba (Cardinal) on Apr 15, 2020 at 14:07 UTC | |
by Luk (Initiate) on Apr 16, 2020 at 11:19 UTC | |
by choroba (Cardinal) on Apr 16, 2020 at 12:30 UTC | |
|
Re: question about creating windows using WX module
by Anonymous Monk on Apr 16, 2020 at 07:22 UTC |