http://www.perlmonks.org?node_id=680002

TomKane has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Perl/Tk because of many powerful features that aren't available in other procedural languages. However, there are still some language eccentricities that I'd like to clear up.

1) Is there some way to turn off the little Tk logo that appears on the caption bar? I'm sure it is not a significant issue for an end user to see it, but why have it on a production application if one can avoid it?

2) When I try to open a new main window via Toplevel, I get what amounts to a second application running, which results in a second icon on the task bar. All I'm trying to achieve is the equivalent of a modal dialog box. BTW, I'm using the form style placement of widgets in my frames.

3) This isn't Perl/Tk specific, but I feel dumb that I haven't figured out how to do this yet: is there a way to create a file of constants like a C ".h" file would contain?

All suggestions and feedback will be appreciated.

Replies are listed 'Best First'.
Re: Perl/Tk Oddities
by Erez (Priest) on Apr 12, 2008 at 18:45 UTC

    2) When I try to open a new main window via Toplevel, I get what amounts to a second application running

    use the Tk::Dialog or the Tk::DialogBox widget:

    $dialog= $mw->Dialog(-title => 'dialog', -text => 'are you sure?', -default_button => 'OK', -buttons => [ 'OK', 'Cancel'])->Show( ); if ($answer eq 'OK') { }

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

      Here is an attempt I have made to achieve the concept of modal dialog box with the Dialog widget. I'm also using an icon in the upper left. It's a simple icon I found that some software had installed.
      #!perl # file form_dialog.pl =begin comment This is a demo to show how to create a Perl/Tk modal dialog box runnin +g from a button entry. This also uses the form layout/geometry manager. Note that a few widget are positioned relative to the center of the di +alog box by computing the required width of the widget and then by taking h +alf of the widget and using that half-width to offset from the center of t +he dialog. If using the -left parameter for placement, the left edge of a + widget will be to the right of that point. Therefore, it is necessary to shif +t a widget to the left of the center ('%50') by half of its width in order + to give it the correct placement. The same can be done for vertical cente +ring. =cut use Tk ; use strict ; use warnings ; use Tk::Dialog ; use Tk::Button ; ###################################################################### my $message = 'nothing yet ...' ; my $w ; #used for horizontal placement of widgets relative to form ce +nter ###################################################################### my $mw = MainWindow->new ; $mw->title('Opens a dialog box from button' ) ; $mw->geometry( "250x100" ) ; $mw->toplevel->iconbitmap( 'Status4.ico' ) ; my $input = $mw->Button( -text => 'Press to input data!', -command => \&demoDialog, -padx => 5 ) ; $w = -( $input->reqwidth()/2 ) ; #here's how to center a button $input->form( -top => '%10', -left => [ '%50', $w ] ) ; my $label = $mw->Label( -textvariable => \$message ) ; $label->form( -top => [ $input, 10 ], -left => '%10' ) ; my $exit = $mw->Button( -text => 'Exit', -command => [$mw => 'destroy'] ) ; $w = -( $exit->reqwidth()/2 ) ; #here's how to center a button $exit->form( -bottom => [ '%95' ], -left => [ '%50', $w ] ) ; ###################################################################### MainLoop ; ###################################################################### sub demoDialog { #must use -buttons, otherwise an 'Ok' is automatically included in + the # dialog box #note that there appears to be no way to position these buttons at + the # bottom of a dialog box in the 'form' sense. They appear to be # amaturishly close to the bottom edge! my $dialog = $mw->Dialog( -bitmap => 'question', -title => 'Demo of Form-based Dialog', -default_button => 'Yes', -buttons => [ qw/Yes No Cancel/ ] ) ; $dialog->geometry( "450x200" ) ; my $txt = $dialog->add( 'Label', -text => "Yabba, Dabba" ) ; $txt->configure( -font => [ -family => 'Courier', -size => 16, -weight => 'bold', ] ); $w = -( $txt->reqwidth()/2 ) ; #here's how to center a widget $txt->form( -bottom => '%50', -left => [ '%50', $w ] #the $w value is negative, so it moves to + left of center ) ; my $lab = $dialog->add( 'Label', -text => "label text" ) ; $lab->form( #this is a label that is right justified -top => '%10', -right => '%90' ) ; my $textent = undef ; my $ent = $dialog->add( 'Entry', -textvariable => \$textent ) ; $ent->form( #a text entry positioned relative to the right edge of t +he label -top => $lab, -right => [ '&', $lab ] ) ; my $bttn = $dialog->add( 'Button', -text => "important stuff", -padx => 5, -command => \&helper ) ; $w = -( $bttn->reqwidth()/2 ) ; #here's how to center a button $bttn->form( -bottom => '%90', -left => [ '%50', $w ] ) ; my $answer = $dialog->Show( ) ; if( ! defined( $textent ) ) { $textent = "nada" ; } $message = qq{You pressed '$answer' and answered '$textent' } ; } sub helper { print "get help\n" ; }
      I had tried the Dialog and DialogBox functions (this was a little while back) and I was stumped for some reason -- which I think was because I was using the form layout style. I'll take another look. Thanks.
Re: Perl/Tk Oddities
by Anonymous Monk on Apr 12, 2008 at 20:01 UTC
    1) Is there some way to turn off the little Tk logo that appears on the caption bar? I'm sure it is not a significant issue for an end user to see it, but why have it on a production application if one can avoid it?
    This won't turn it off, but will replace the logo with one of your own, which might be blank to achieve the 'turned off' effect. In place of the -file attribute, one can use the -data attribute to use an xbm bitmap defined somewhere in your script.

    Here's a snippit of non-standalone code as an example.

    $mainwindow->Icon( -image => $mainwindow->Pixmap( -file => 'c:/perl/site/lib/tk/Dubya.xpm' ), );
      Thanks. I was able to create a blank xpm file that blotted out the TK logo. Now I'll look into creating my own logo. I didn't catch on to the fact that the Icon() function worked on the upper left part of the caption bar, I thought it was oriented toward the task bar.
Re: Perl/Tk Oddities
by moritz (Cardinal) on Apr 12, 2008 at 16:40 UTC
    3) This isn't Perl/Tk specific, but I feel dumb that I haven't figured out how to do this yet: is there a way to create a file of constants like a C ".h" file would contain?

    Your best bet is to write them into a module.

    Or you can just dump them in a YAML or XML file and use a module to read those.

      I guess the module that works as read-only would be the ticket. It's a little clumsy though. But thanks.
Re: Perl/Tk Oddities
by TomKane (Beadle) on Jul 22, 2008 at 14:27 UTC
    I've finally gotten back to trying to force Perl/Tk to operate with professional dialog box functionality.

    The problem with Tk::Dialog and Tk::DialogBox is that they both want to manage the closing buttons in a separate bottom section of the dialog box. From my understanding, there is no way to intercept the results of the bottom buttons. Actually, there is a built-in mechanism through the -command option, and that is where you can save data, etc. But once the bottom button is clicked, you can't cancel it (for example, if the user forgot to enter a critical field) and force the dialog to stay open.

    The good thing about Tk::Dialog and Tk::DialogBox is that they don't appear to create separate applications with icons showing on the bottom of the display (in Win32). This is despite the fact that both are derived from Tk::Toplevel.

    As I mentioned earlier, Tk::Toplevel provides a method for building completely functioning dialog boxes with total control of all widgets and processing. There is a drawback, however, in the fact that every Tk::Toplevel is treated by Tk as if it were a separate application, which shows up as an icon on the bottom of the display.

    The only workaround I've been able to come up with is to use the toplevel->withdraw() function in both the calling module (when it calls a module that has its own respective Toplevel) and then in the called module when it closes down. The crazy thing about this is that the callee must have a link back to the caller, i.e., a subroutine call, to tell the caller that the callee has completed its processing and withdrawn itself, and that it's time for the caller to deiconify() and raise() itself.

    The use of withdraw() in this way prevents the situation where there are multiple application icons. It's not perfect, however, as I can literally see the withdrawing of one module and the raising of another as the icons repaint themselves at the bottom of the display. But that's a small price to pay for having the control of the Toplevel serving as a dialog: I can completely manage the exiting process and I fully control the positioning of all widgets within the Toplevel (whereas with Tk::Dialog and Tk::DialogBox I have no control of the button placement).

    If anyone has any suggestions or insights where I may be making a mistake or may be overlooking some option that might make the Tk::Dialog and Tk::DialogBox processing work better, please let me know. Thanks.

    Tom

      Funny, I was just working on ways to do these dialog tricks with Tk and Gtk2. For Tk, I came up with Tk RGBColorDialog as an example of using a toplevel as a dialog. To avoid the problem of the predefined Ok button, yet still wait for a response in a Show() method, I used a waitVariable hack in Show(). It works by having the $self->{RESULT} being set to '', and when the Ok button is pressed, it is set to $self->{value} (the return data). You probably can work out a way in Show to test the value before returning it. Like this simple way with a LABEL (there is probably a better way)
      WAIT: $self->waitVariable(\$self->{RESULT}); #test result my $string = $self->{RESULT}; if( $string lt '#333333' ){ goto WAIT } else{ $self->grabRelease; $self->withdraw; &$old_focus; &$old_grab; return $self->{RESULT}; } } # end Show method
      In Gtk2, the preferred method is to wrap the dialog like this. It can be done because the destroy is handled by you outside of the Dialog object. Thanks to muppet and Mathew Braid, for showing me this.
      sub get_string { my ($title, $parent, $prompt, $initial_value, $default_cancel) = @_; my $dialog = Gtk2::Dialog->new ($title, $parent, [], 'gtk-cancel' => 'cancel', 'gtk-ok' => 'accept'); my $label = Gtk2::Label->new ($prompt || ''); $dialog->vbox->add ($label); $label->show (); my $entry = Gtk2::Entry->new (); $entry->set_text ($initial_value) if defined $initial_value; $dialog->vbox->add ($entry); $entry->show (); $dialog->set_default_response ($default_ok ? 'accept' : 'cancel'); my $ret; if ('accept' eq $dialog->run ()) { $ret = $entry->get_text (); } $dialog->destroy (); return $ret; }
      It may be possible to subclass the Tk::Dialogbox, and override the default button presses. It has the wait Show and exit methods available, so you should be able to redefine them in a subclass, use
      use base qw/Tk::Derived Tk::DialogBox/;
      You can google for Tk::Derived and find examples how to do it. Basically you just make a package called MyDialog, copy the Show() sub from the Dialogbox.pm into it, and modify it to your liking. You will see where I got the idea for my code in the Dialogbox.pm. :-)

      Probably the best solution, is to emulate Gtk2's wrapping of the dialog, by redefining exit() in your subclassed DialogBox, so it is controlled from outside the object.


      I'm not really a human, but I play one on earth Remember How Lucky You Are
        Thanks for the feedback, Zentara. You led me indirectly to the answer that I was looking for.

        As I was formerly a C++ programmer, I am familiar with the concept of overriding functionality. I just hadn't done it in Perl and had been hesitant to start digging down to that level when it came to base functionality of the language. But I started looking into some of the DialogBox.pm code and then discovered XDialogBox.pm. As I was looking at that bunch I discovered that whenever I used an XDialogBox widget, it always resulted in having an extra icon at the bottom of the screen, which is exactly what I've been trying to avoid. Then I saw that there were a couple of commented out lines of code and tried uncommenting them. It turns out that the Toplevel->transient() function was one of them and when I turned it back on, voila, no icon.

        So, the quick and simple answer to my long search was there all along. Even in "Mastering Perl/Tk", Lidie doesn't mention that particular functionality (the index has the wrong page number, it's really p. 246) for making a new Toplevel appear as a part of the main. That is, the new Toplevel is a "transient" but detached widget within the "master" module that opened the new Toplevel.

        Now the fun begins, however, because if I use a Toplevel as a dialog box, then I have to have a button or something that will "close" the dialog -- which is done really with the withdraw() function. But this doesn't tell the "master" module that it's closed. As long as the "master" is itself a module and can have a function accessible to the transient Toplevel, the latter can call the former with the message that it (the transient Toplevel) has "closed".

        I better look into the grab() methods now.

        Thanks again.

        Tom