Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Is there a better way to learn Tk?

by ELISHEVA (Prior)
on May 08, 2009 at 05:00 UTC ( #762748=perlquestion: print w/ replies, xml ) Need Help??
ELISHEVA has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get up to speed with Tk and finding it hard going. To translate my knowledge of other GUIs to Tk I really need something like a systematic overview that says: Hey, Tk sort-of, but not quite, borrows its terminology from Motif (e.g. Geometry manager) but don't look for Combobox in the documentation because we call it "BrowseEntry". (go figure); the core widget set is ... here are the five programmable events handled by the Foobar widget and so on. Here is how to control the event loop...

The CPAN documentation seems plentiful but incomplete. For example, if there are parameters for Tk::MainLoop or any special methods for Tk::MainWindow it can't be discerned from the documentation.

Studying Perl source code is time consuming but even that isn't an option, since most of the Tk library is just a wrapper around a C library.

Often times a Perl wrapper for a cross-language library has sketchy documentation, so usually I look for a good cross-referenced API in the original language. But if such an animal exists, my Google fu isn't strong enough to find it. Maybe a reference to it is hiding somewhere in the CPAN documentation? The closest thing I could find was Tk API manual but it isn't very searchable - just a long list of manual pages. Furthermore, I don't have a clear idea of how any of this is mapped back to the Perl Tk without exploring the C code itself (and I can't even do that because I'm not sure where to find Tk.xs and tkGlue.c the two files supposedly responsible for connecting the C libraries to Perl, according to Tk). CallingTk is "under construction".

Most of yesterday I muddled through using CPAN and a tutorial here and there, but this isn't a very efficient way of learning something. Tutorials tend to focus on explaining concepts, not in laying out the full range of capabilities of a toolkit.

I feel like I'm missing something obvious - a Google link that was staring me in the face, perhaps?

Many thanks in advance, beth

Comment on Is there a better way to learn Tk?
Re: Is there a better way to learn Tk?
by pobocks (Chaplain) on May 08, 2009 at 05:36 UTC

    I'm not sure if this is useful, but there are two O'Reilly books for Perl/TK, the lower-level of which I found fairly useful, albeit basic.

    I'm pretty sure they're called Learning Perl/TK and Mastering Perl/TK. I've found the first book in multiple libraries.

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      Mastering Perl/Tk is more than useful and far more complete than the CPAN documentation. It is good you mentioned it. In fact, it is a part of the reason I know that the CPAN documentation is woefully incomplete.

      But like most tutorials, they are primarily concerned with explaining the concepts (how to write a GUI) rather than laying out the range of tools available. Some fairly common use cases are missing: for example, how to create a list box or combo box where the user can add elements to the list. Is this something that has to be created out of lower level components? Is there a core widget with this functionality built in? Part of what I need to do is assess how much custom programming will be needed and how much can rely on existing components. That is hard to do without an overview laying out the capabilities of Tk.

      Best, beth

      Update Link removed: apparently not legal - JavaFan and private communication. O'Reilly does provide on-line access to this book, but they have changed their format and now require you to subscribe to their Safari On line book service to get access.

        Mastering Perl is available on line through Mastering Perl/Tk. Whether this is a legal copy or not,
        Let's see, the link points to Ukrainian domain, and the URL misspells the name of the publisher.

        What would your guess be about its legal status?

Re: Is there a better way to learn Tk?
by zentara (Archbishop) on May 08, 2009 at 10:44 UTC
    "is there a better way?"

    Yes, Perl/Gtk2. Gtk2 has a pretty good object inheritance( who am I to judge OO style :-) ). Everything descends from a GLib object, and Glib itself is not dependent on X.

    The problem with Gtk2 is that the docs are auto-generated......that way they can keep the current libs and docs up-to-date, just by running a program that autogenerates everything (which is then checked for bugs by everyone). This makes the docs very terse, but there is an inheritance tree included in all the docs....and just as its supposed to work..... each descendant inherits the stuff from the ancestor. Yeah !! It actually works!!

    Tk, on the other hand, is a collection of independent widgets (that have been glued together remarkably well by the TCL/Tk libs). The perl port adds another abstract layer ontop of the TCL/Tk, where signals/etc can get lost. I sort of look at Perl/Tk now, as the "Windows95" of guis. Very cool and easy to make simple guis.....but way behind the times.

    Nick Ing Simmons was the Master behind Tk, and he got sick and died a few years ago, may he RIP. Since then, only sporadic attention has been paid to Tk, by a few C/Perl gurus. So Perl/Tk is probably headed for the history bin. If you really like the Tk style( as opposed to Gtk2 or Wx), of programming.....switch to TCL/TK. I hear it is pretty up-to-date, but it isn't Perl. ( There is a monk around here called Vorkon ??.... who promotes a Perl module for Tcl?Tk.

    I would only write Perl/Tk now if it was an extremely simple quick app, or it was patching some old code.

    The best way to learn either Tk or Gtk2, is to use groups.google.com to search for examples of the widgets you want to use. I find jumping right into examples, is faster than reading a book. If you do resort to a book....get an online version or cd version, so you can cut-n-paste code snippets, and run the examples. 50% of it all, is just learning how to avoid the common runtime mistakes, like declaring modules properly, getting commas and fat arrows correct, etc. The more test programs you run, the more error messages you will see ( :-) ) , and then you learn to quickly fix problems.

    I found some really weird Tk syntax errors, that can be totally perplexing......where you get an error like "not valid at mainloop line 67"...... and the error would be caused by some syntax error in pack, where you do something like make an => a = and the Tk part of the program dosn't throw an error.

    P.S. Howdy. .. to my Finish cousin :-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      I sort of look at Perl/Tk now, as the "Windows95" of guis.
      More like Windows3.1 with unbelivably long lifetime
Re: Is there a better way to learn Tk?
by Marshall (Prior) on May 08, 2009 at 15:33 UTC
    The CPAN documentation seems plentiful but incomplete. For example, if there are parameters for Tk::MainLoop or any special methods for Tk::MainWindow it can't be discerned from the documentation.

    I have the Perl/Tk Pocket Reference by Stephan Lidie. I counted 27 methods that can be called on a mainwindow in that chapter. Appendix A of Learning Perl/Tk has a table of common widget methods. But that book is out of print. I don't know about the Pocket Reference. But is very handy and has a lot more than the Appendix A table. This isn't a tutorial, it is a "cut to the chase", here is a widget and here are the methods. There aren't any examples.

    Sorry wrong button duplicate...see below...

Re: Is there a better way to learn Tk?
by Marshall (Prior) on May 08, 2009 at 15:38 UTC
    The CPAN documentation seems plentiful but incomplete. For example, if there are parameters for Tk::MainLoop or any special methods for Tk::MainWindow it can't be discerned from the documentation.

    I have the Perl/Tk Pocket Reference by Stephan Lidie. I counted 27 methods that can be called on a mainwindow in that chapter. Appendix A of Learning Perl/Tk has a table of common widget methods. But that book is out of print. I don't know about the Pocket Reference. But is very handy and has a lot more than the Appendix A table. This isn't a tutorial, it is a "cut to the chase", here is a widget and here are the methods. There aren't any examples.

    Update: http://www.amazon.com/Perl-Pocket-Reference-Stephen-Lidie/dp/1565925173/ref=sr_1_3?ie=UTF8&s=books&qid=1241797000&sr=1-3
    $10 on Amazon. I recommend this.

Re: Is there a better way to learn Tk?
by renodino (Curate) on May 08, 2009 at 17:39 UTC
    As others have mentioned, Perl/Tk may not be your best solution. Have you considered Tkx ? IIRC, our friends at ActiveState are pretty actively maintaining/using it.

    As for books, "Mastering" is the best bet; I once saw "Learning" described as "quite possibly the nadir of O'Reilly books"...and I concur.


    Perl Contrarian & SQL fanboy

      Do you know what the performance/requirements difference between the two packages is, by any chance? What I can find seems to indicate that Perl/Tk directly wraps the C code for Tk, whereas Tkx wraps Tcl calls Tk. I can't imagine that not causing some performance difference.

      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
        perl/Tk is probably faster, but Tkx is more up to date in terms of features/widgets and will run on perl 5.6.
Re: Is there a better way to learn Tk?
by Argel (Prior) on May 08, 2009 at 19:28 UTC
    I feel like I'm missing something obvious.
    One would think that Perl is well suited for GUI development considering how versatile the language is. But as you have noticed, the reality is that GUI development in Perl is a weakness instead of a strength. I have long wished for a nice IDE that lets me design my GUI, generate the GUI code for me, etc. But instead Tk variants seem to still be the most popular, along with WxWindows, and more recently Gtk (but, being a KDE fan I would prefer something for Qt). It's a sad and embarrassing state of affairs. Not to knock the work of the people on these projects (some of which I have used), it's just that it is painfully obvious that we could -- and should -- have something much better.

    Elda Taluta; Sarks Sark; Ark Arks

      The same thing could be said for Java GUI libraries as well. There are a number of issues that every GUI library has and generally there few languages that include a good Visual Design Suite with them(AKA MS Visual Studio).

      Personally, when it comes to Perl and GUI's I believe HTML and web browser are better all around. They are more flexible and various libraries are being written that are making them easier to work with(like JQuery). While a browser cannot be used for everything I would say there are a few situations where you need stand-alone application on a desktop.

Re: Is there a better way to learn Tk?
by graff (Chancellor) on May 08, 2009 at 22:20 UTC
    If the goal is to get acquainted with Perl/Tk in particular, the method that put me well on my way very nicely (oh those many years ago) was to just play with and study the demo program called "widget" that is included in the Tk distro.

    It really is a very nifty piece of work -- you can browse lots of different widgets (all the important ones, and a fair number of "special" ones), explore different kinds of options and controls, and use the tool itself to look at and even modify the source code while you're playing with it. Just run "widget" from your shell.

    Also, look for and try out Tk apps that have been posted here at PM, for a broader sample of coding styles. (You may have noticed that our own zentara is a master of the Tk arts.) Coincidentally, one of my first posts at the monastery was a "fun with Tk" thing: Tk cryptogram (no, not cryptography) -- in fact it was my own "teach yourself Tk" app.

      Infinithanks for the pointer to "widget" - that looks insanely helpful.

      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
Re: Is there a better way to learn Tk?
by hangon (Deacon) on May 09, 2009 at 02:18 UTC

    Despite some of the criticisms mentioned here, and including my own frustrations, my opinion is that Perl/Tk is still worth learning. I've tinkered a bit with some others and with the possible exception of Prima it seems to be the most Perlish. Once you get the hang of it, Perl/Tk is not very difficult at all. OTOH I really want to like Wx, but trying to learn it gives me fits and leaves me cross-eyed with a headache.

    The collective Perl/Tk docs actually contain sufficient information scattered about to get you going, but its a bit difficult to sort through. The following PODs should cover most of what you need to get started. Also, there should be a lot of example code somewhere under @INC at Tk/demos/.

    • Tk::UserGuide - This gives the basics and general overview. It also contains a list of standard widgets you should familiarize yourself with. Complex widgets are generally built with some combination of these.
    • Tk::option - Basic introduction to widget options database & methods
    • Tk::options - The standard widget options.
    • Tk::Widget - Generic methods for widgets.
    • Tk::pack - The most common and easiest geometry manager, its best to start with this one. There are also grid and place and form geometry managers.
    • To build complex/custom widgets or subclass existing widgets:
      • Tk::mega
      • Tk::composite
      • Tk::Derived
      • Tk::ConfigSpecs
    • Events, callbacks & binding:
      • Tk::callbacks
      • Tk::bind
      • Tk::bindtags
      • Tk::event
      • Tk::after

    Update: Here's a pdf Perl Tk reference designed to be printed & folded into a booklet. Its for an older version, but may be helpful to you.

Re: Is there a better way to learn Tk?
by quinkan (Monk) on May 09, 2009 at 04:33 UTC
    In practice, I've found the easiest way to work in this area is with a combination of things already mentioned or eluded to:
    • widget demos are a handy start
    • Mastering Tk adds things not obvious in the demos
    • tools that generate code fill in most of the simple areas (vtk, specperl, glade, ZooZ). You'd be silly to rely on them completely, but they can give a start.
    There's also the tutorial here with pointers to stuff... .
Re: Is there a better way to learn Tk?
by quinkan (Monk) on May 09, 2009 at 04:36 UTC
    In practice, I've found the easiest way to work in this area is with a combination of things already mentioned or eluded to:
    • widget demos are a handy start
    • Mastering Tk adds things not obvious in the demos
    • tools that generate code fill in most of the simple areas (vtk, specperl, glade, ZooZ). You'd be silly to rely on them completely, but they can give a start.
    There's also the tutorial here with pointers to stuff...
Re: Is there a better way to learn Tk?
by Limbic~Region (Chancellor) on May 11, 2009 at 18:26 UTC
Re: Is there a better way to learn Tk?
by ELISHEVA (Prior) on May 12, 2009 at 16:51 UTC
    Thank you, all for your many helpful responses.

    zentara I am taking a good look at Gtk2. At first glance it seems much more friendly to my learning style and project scoping needs than does Tk. The documentation coupled with the tutorial give me most of what I need for a quick assessment of capabilities.

    Comparing Tk and Gtk2 really does make one aware of how much our approach to GUIs has changed in the last 20 years. One of the first APIs I ever designed and had to train programmers to use was an event driven TUI (text origined user interface) written in 1989 just before Windows 3.0 came out. Even on Microsoft systems, we were still using DOS back in those days. Since then I've worked intimately with an old, old version of Motif, both C and C++ versions of the Microsoft GUI API, the NeXT API, various Database GUIs (Access, Oracle, Progress), and most recently the Java Swing and AWT APIs. Until now I've never had a chance to step back and see the changes.

    The biggest difference I see isn't the prettiness of the widgets but the way we work with them. Back in the late 80's and early 90's widget dynamics were, for the most part, a two way dialog between the user and the widget. For the programmer a widget was basically a pretty picture painter and data box. Programmable events were usually very very low level (mouse and key strokes) or very high level (respond to this command). The Tk documentation reflects this tradition. Documentation is very heavy on lists of properties, but light on events associated with each widget.

    In the last 20 years widgets have developed a host of logical events somewhere between key strokes and commands. This was not easy to do. For example, many of the early APIs didn't always do a good job distinguishing between the different reasons for leaving a field. I seem to recall that the Progress database GUI had a bug where attempting to leave a field with an invalid value to press a help button caused a dialog box telling you to go back to the field and fix it. No matter what you did when you closed the dialog box, you were forced back into the field with the bad value. Hotel California in an edit box. (You can checkout any time you like, But you can never leave!)

    Today, the situation is much improved. In a modern GUI API, a typical edit widget has well defined events to tell us when it gets keyboard focus, when it is about to set a value, when it has already set a value, when it is loosing focus. There are different events for leaving a widget and leaving the group that contains it, and leaving a dialog box as a whole. At each of these points we can stop the process, or do something special and continue it. If the widget is a list box, we will have events that tell us when an item is selected, deselected, moved up or down in the list box order, edited, moused-over on an icon, and so on and so forth.

    The result is a three way dialog between user, widget, and programmer. The Gtk2 tutorial reflects this newer way of working with widgets. In addition to an over view of events as a whole, each widget article lists its main events along with the properties that can be set. It isn't just about pictures and data any more.

    Pobucks that widget demo is awesome. It is about as close as anything to a survey of the capabilities. And yet... I still don't know what events I need to intercept to edit a list of items. Ah well... am I the only person on the planet that wants a widget where I can add items to a list rather than just pick items from a list? Grrr.

    Limbic~Region thank-you so much for the empathy. Yours and my blood have mixed on that wall.

    Best, beth

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://762748]
Approved by planetscape
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-08-30 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls