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

I am using Perl/Tk to create a little calculator program, just as practice.
As part of the calculator, I have a selection in one one of the menus labeled "Scientific mode" which
has such features as sin, cos, and tan.

I have put the entire scientific section into a subroutine so I could run it and generate the extra buttons, etc. The question I had is whether there is a way to make the scientific buttons disappear if the user clicks the button again.

The source is much too long (about 600 lines) to include here, but you can find it at http://joel.nackman.com/downloads/calculator.txt

Also, any other suggestions are welcome.

Thanks,
Joel

Replies are listed 'Best First'.
Re: Perl/Tk question
by K_M_McMahon (Hermit) on Feb 09, 2005 at 05:01 UTC
    Hi Joel, Welcome to the Monastery. Hope you stick around for a while. A few comments.
    1) add use strict and use warnings. It is just good practice to help keep your errors to a minimum (especially in Tk.
    2) You can post even large bits of code, read the FAQ on posting and use the READMORE and CODE tags.
    3) I would make the sub "scientific" create a new FRAME:
    $scientific_frame =$main->Frame(-background=>'tan', -foreground=>'black', -relief=>'groove', -borderwidth=>3 )->pack(-side=>'bottom',-fill=>'both');

    Then, modify (using "configure") The original menu option to call a different sub which does $scientific_frame->destroy(); then reconfigure the menu to call the original subroutine again if they want to turn scientific back on.
    Have fun in the monastery!
      Even better is to toggle the "visibility" of the frame using pack (to make visible) and packForget (to make invisible).
        Hi!

        You can also do this with grid, gridForget (I'm just mentioning it because joelnackman uses grid).

        Regards, mawe

      In my experience, I have found that Tk's destroy() method chews up (or leaks) memory. My general practice is never to use destroy() and implement "appearing" and "disappearing" of anything using one of the other methods posted below, especially if this is an app that is going to be changing its state many times in a single execution.

      Note: About the grid manager, as seen on node Tk::grid and gridRemove(), gridRemove() and gridForget() are NOT identical :-).

Re: Perl/Tk question
by saintmike (Vicar) on Feb 09, 2005 at 05:05 UTC
    To grey out the "cos" button, use
    $cos->configure(-state => 'disabled');
    with $cos being the Tk::Button object. To be able to enable it again when cos is valid input again, you need to maintain the state of the math input in a variable.
Re: Perl/Tk question
by zentara (Cardinal) on Feb 09, 2005 at 13:08 UTC
    You might like Tk::CollapsableFrame. It could be packed with your buttons, and collapsed and expanded as needed. You can also modify it to suit your needs. Here is an example. You can also open and close frames from your program programatically.

    I'm not really a human, but I play one on earth. flash japh

    Updated Steve_p - added readmore tags

Re: Perl/Tk question
by blueberryCoffee (Scribe) on Feb 09, 2005 at 10:04 UTC
    Just use pack and packForget methods. This will hide, because the pack geometry manager no longer considers the widget.
Re: Perl/Tk question
by aquarium (Curate) on Feb 09, 2005 at 11:28 UTC
    just destroy the buttons, and create/pack when the scientific feature is turned on. the scientific feature should be a flip-flop variable bound to the scientific button. and the button's click should be bound to the routine to create/destroy all the scientific calc buttons, depending on the value of the flip-flop variable.
    the hardest line to type correctly is: stty erase ^H