Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Gtk2 + GladeXML

by Eyck (Priest)
on Jul 26, 2004 at 06:35 UTC ( #377365=note: print w/replies, xml ) Need Help??


in reply to Best GUI package for Perl ?

I find GladeXML-based programming very refreshing, I used to churn code based on Tk, and this code was a real spaghetti, gui stuff mixed with actions mixed with logic ... horrible.

With Gtk2 and GladeXML you write your code, and seperately you draw your application, glade spews xml describing how your app should look, and you write what this app should do.

And this work equally well under UNIX oses and under windoze ( although on windoze you have to go through painfull process of installing gtk and gladexml - in my case this requires installing compilers etc... ). And the code can look like this:

use Gtk2; use Gtk2::GladeXML; #This loads description of your GUI, from file named #app.glade open (GLADE, "<", $sharedir.$execname.'.glade'); my $gladebuf; while (<GLADE>) { $gladebuf .= $_; } close (GLADE); $gladexml = Gtk2::GladeXML->new_from_buffer($gladebuf); # # This way you get the object you want to work on: my $window=$gladexml->get_widget('window'); # There's nothing stopping you from doing this on-the-fly: $gladexml->get_widget('userName')->set_text($userName); ############ sub on_something_something { my $w=$gladexml->get_widget('SomeWidget'); $w->show_all(); $w->run(); $w->hide(); } # Gtk2->main; exit 0;

And now, you can easily modify your apps look without touching the code, for example, you can take some window and turn it into a tab, or reorganize forms etc etc... This sounds like a great thing for teams with separate UI-designer, but this kind of development feels great even for single person, it's just so much easier not having to think about both functionality and looks at the same time (even if you switch from one to another every second, it's easier to work if you've got two virtual desktops dedicated - one for code, another for GUI).

Replies are listed 'Best First'.
Re: Gtk2 + GladeXML
by rbutcher (Beadle) on Jul 27, 2004 at 11:06 UTC
    "I used to churn code based on Tk, and this code was a real spaghetti, gui stuff mixed with actions mixed with logic ... horrible"... !! Thanks Eyck and all the other guys for your feedback. I just finished my first effort with Perl/Tk and I ended up with 600 lines that look just as you describe above, even though I think I understand the event handler concept... I just couldn't separate stuff like freezing/unfreezing buttons, from stuff to fire up and monitor the background process (via 'After' callback), from stuff to load and validate parameter files, from error handling (very important that absolutely any problem must produce coherent output), from stuff to cleanup zombies on process cancellation... horrible. I got Perl/Tk to do everything I wanted, but it is unmaintainable code. So big plus and big minus. Anything out there to force some function separation into Perl/Tk ? Meantime I'm looking at Perl/Qt (nice looking design tools, but what a name - Puic !?). Then I'll look at Glade/Gtk, sounds interesting. thanks & regards Rod
      You're asking for function separation in Perl/Tk? Well, just do it! It's the same as in normal non-GUI programming. You can use subroutines, you can use modules. Granted, there are some nice features in Tk which allows for writing quick scripts (e.g. -variable, anon subroutines in command bindings), but nobody forces you to use these features.

      This is just like people complaining about Perl to lead to unmaintainable code. You can write unmaintainable code in both Perl and Perl/Tk, but you can also write nice code if you use the language and module wisely.

        What you propose is rewriting GladeXML but using perl instead of xml for interface description.

        And where are those tools that would allow drawing GUI with your proprietary perl interface?

        The problem is that GUI requires different style of thinking/coding then general problems ( that's one of the reasons why GUI programmers tend to think that OOP is so great - o yeah, it's great for writing GUIs, not always so great for solving general problems )

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-10-20 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (81 votes). Check out past polls.

    Notices?