Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Using Wx with elements from XRC (XML resources)

by tsee (Curate)
on Nov 01, 2006 at 12:40 UTC ( #581666=perlquestion: print w/replies, xml ) Need Help??
tsee has asked for the wisdom of the Perl Monks concerning the following question:

Dear fellow monks,

Everyone who has ever written GUI applications knows that designing them in code can be a pain. Fortunately, using / wxWidgets (and some other GUI toolkits), it's possible to load the design of your GUI from an XML file which you previously generated using a GUI designer such as wxglade.

Sadly, the documentation for this procedure is lacking from the Wx Perl docs altogether and doesn't seem to be overly verbose in, say, the Python Wx documentation either. But I'm digressing!

I created a custom Wx Panel using wxglade which contains just one button for demonstration purposes and exported the results into, say, t.xrc.

<?xml version="1.0" encoding="UTF-8"?> <!-- generated by wxGlade 0.4.1 on Wed Nov 1 13:21:42 2006 --> <resource version=""> <object class="wxPanel" name="panel_1" subclass="MyPanel"> <style>wxDOUBLE_BORDER|wxTAB_TRAVERSAL|wxFULL_REPAINT_ON_RESIZ +E</style> <object class="wxBoxSizer"> <orient>wxHORIZONTAL</orient> <object class="sizeritem"> <flag>wxADJUST_MINSIZE</flag> <object class="wxButton" name="button_1"> <size>185, 128</size> <label>button_1</label> </object> </object> </object> </object> </resource>

Since I want my panel to behave a little different from the default Wx::Panel, I specified that its class is 'MyPanel' in the wxglade editor. After all, support for this kind of thing (subclassing) is a central point of the Wx OO interface. In my Perl code, I implement the MyPanel package as a subclass of Wx::Panel as required.

#!perl -w use strict; use Wx; use Wx::XRC; package MyPanel; use base 'Wx::Panel'; sub new { my $class = shift; my $self = $class->SUPER::new(); $self->OnInit(@_); return $self; } sub OnInit { my $self = shift; my $parent = shift; my $xrc = shift; $xrc->LoadPanel($parent, 'panel_1'); return $self; } package MyApp; use base qw(Wx::App); sub OnInit { my $self = shift; my $frame = MyFrame->new( undef, -1, "Hello World"); $frame->Show(1); $self->SetTopWindow($frame); return $self; } package MyFrame; use base qw(Wx::Frame); sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); $self->{xrc} = Wx::XmlResource->new(); $self->{xrc}->InitAllHandlers(); $self->{xrc}->Load('t.xrc'); $self->{panel} = MyPanel->new($self, $self->{xrc}); return $self; } package main; my($app) = MyApp->new(); $app->MainLoop();

Running the script, I see the main window with my custom panel (and button) as expected, but I also get an error message in a dialog box: Subclass 'MyPanel' not found for resource 'panel_1', not subclassing!. The only exact mention I found of this was in the sources of wxWidgets itself. That was way too C++y for me, though.

The wxPython docs mention something about a necessity to map the (Perl or Python) class name to the class name as XRC sees it. Finally, I found some mention of a similar problem in the wxperl mailing list: original post, first answer, second answer. Basically, this feature was missing from Wx before 0.16. Mattia promised to add it in 0.16 and judging from the ChangeLog, he did. Still, I'm not having any success with the most recent Wx. I cannot find any documentation either.

What am I doing wrong? Any Insights?

Thanks for reading,

Update: Spelling

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://581666]
Approved by Corion
Front-paged by holli
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2017-06-25 01:17 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (564 votes). Check out past polls.