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

Greetings,

I apologize for taking everyone's time with my silly questions, but I'm having a very difficult time figuring out how to structure a Perl/tk program that is more than a single screen. I'm sure someone has already dealt with these issues before, and I'm looking for some suggestions (dare I say best practices).

For example:

For a simple program that consists of a main menu with two options (which each lead to very simple data entry screens), this is what I believe I would need. To make my language less ambiguous, I will assume that each "screen" consists of a single frame.

It looks like I should define my frames first - one for each screen.

Frame0 : main menu screen Frame1 : option 1 screen Frame2 : option 2 screen
And of course I define all of the various widgets and callbacks that are contained in each of these screens.

Then, I have a subroutine that takes two parameters, the existing frame and the new frame. This is the frame-switcher. It packForget(s) the existing frame and pack(s) the new frame. This frame-switcher will be called from the main menu (to select one of the two screens) or from either of the data entry screens when the user needs to get back to the main menu.

But before I display the new frame, I really need to clear all the values that might exist in all the widgets of that screen. So I need a way to either clear all the values of the widgets on that screen before re-displaying it or a way to delete the widgets and re-define/re-pack them onto that frame.

Which of these options makes the most sense? Or is there a third (or fourth) option. I apologize if this is confusing (it is to me).

To make matters more "interesting", one of my screens consists of a variable number of frames (depending on how many "items" the user added to an invoice). So I need a way to re-set it back to 1 item before I re-display it. I don't want to have to pull all my variables out from the guts of the GUI and make them global just to clear them.

It seems difficult to avoid global variables with GUI programming, but I believe I should... right? It appears that all my variables will need to be visible from the frame-switcher (which will need to clear them somehow).

I'm having a very hard time letting go of the sequential execution of statements that I grew up with and trying to define everything at once and then just let go with a single MainLoop working it's magic.

Any ideas or examples of multi-screen programs and how someone handled these issues?

Many thanks for any suggestions.

- confused and befuddled

Replies are listed 'Best First'.
Re: Perl/tk program structure for multiple screens
by Khen1950fx (Canon) on Apr 02, 2008 at 06:36 UTC
    I think that this article, Intemediate Widgets, will help you get going. It covers everything from multiple windows, subwindows, radio and check buttons to menus.
Re: Perl/tk program structure for multiple screens
by Anonymous Monk on Apr 02, 2008 at 04:53 UTC
Re: Perl/tk program structure for multiple screens
by zentara (Cardinal) on Apr 02, 2008 at 13:58 UTC
    In general, in a big program like you propose, your main concern in design is to prevent unwanted memory gains from too many widgets laying around unused/discarded. So think "reuse all widgets".

    So I need a way to either clear all the values of the widgets on that screen before re-displaying it or a way to delete the widgets and re-define/re-pack them onto that frame.

    It depends on the widget, but every widget can be emptied of it's current data, and refilled. For instance the Listbox, can use an array reference as it's -textvariable option. So all you need to do is change the array and the Listbox will change. Photo widgets can be reused with the blank and read method. If you change the Photo widget's data, it will automatically change in any widget that displays it. Photo objects are the number one culprit in wasted memory, so reuse them. Similarly, Canvas and Text widgets can be blanked out and reused.

    If you need help on how to reuse any widget, post a node about that particular widget.

    If you want to see an example of a complex juggling act of widget reuse, see ztkdb-sql In that I have a predetermined number of widgets, which I constantly update with new data. It will run all day with no appreciable memory gain.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum