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

Hi all,

Using TK created a MainWindow which is constructed in part from what it pulls from a database (MLDBM database). From the menus of the program the user can then input some additional program values that are stored into the DB. These new values are then used on the next runnig of the window and included in the MainWindow.

So far ... upon restarting the program the MainWindow reflects the new CheckButton additions from the database, but I want it to dynamically create these widgets while the program is still running. So that when a user adds the item from the menu the MainWindow is updated on the fly. There obviously is a way to do this.

For now ... I create the buttons in a subroutine (as below) and recall the subroutine when the user saves to the database.

I assume I have to create a frame object and refresh that after a user adds an item to the database. How do I do this? Something is not quite right in my thinking here.

Dean

my $shiftselect; my $shiftsframe = $topframe->Frame->pack; &displayshifts; sub displayshifts { ## Display all shift types and create Radiobuttons for each + my ($ra, $rb); while (($ra, $rb) = each (%db_sh)) { $_ = $topframe->Radiobutton(-text => $db_sh{$ra}->{'ShiftName +'}, -variable => \$shiftselect, -value => $ra ) -> pack; } }

Replies are listed 'Best First'.
Re: TK - Updating Main Window after user input
by zentara (Cardinal) on Jan 07, 2004 at 17:29 UTC
    I only have a clue as to what you want to do. The following snippet, shows you how to programatically modify menus. Modify it to add more entries.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $count = 0; my $mw = tkinit; my $menubar = $mw->Menu(-type => 'menubar'); $mw->configure(-menu => $menubar); my $menu = $menubar->cascade(-label => '~File'); $menu->command(-label => '~New'); $menu->command(-label => '~Open'); $mw->Button( -text => 'Push me and look at the menu', -command => sub { my $state = ($count++ & 1) ? 'normal' : 'disabled'; $menu->cget(-menu)->entryconfigure(2, -state => $state); } )->pack; MainLoop;
Re: TK - Updating Main Window after user input
by Anonymous Monk on Jan 07, 2004 at 17:18 UTC
    Further update ... I got the detele working but not as efficiently as I'd like. To do so, the MainWindow is created using a grid, the grid containing the frame I'd like updated I destroy and then recreate it.

    There must be a better way of just removing the required object rather than destroying the whole frame! Ideas anyone?

    Dean
      If I understand you, you're talking about controlling the number of RadioButton widgets present/visible on the GUI, according to the number of entries in a database, and changing the number of buttons at run-time, according to database entries being deleted or created by the user.

      This is the sort of thing I would tend to put in a drop-down or pop-up menu, or in a listbox (where setting "selectMode" to "single" has the effect of making the listbox items behave like radio buttons). It's relatively easy to add or remove elements from a menu, and the pop-up or drop-down behavior means that changes in the set of buttons can go on "behind the scenes". It's really easy to add or remove items from a listbox, which has the added advantage of providing a simple scrolling mechanism (see Tk::Scrolled). Either a pull-down menu or a listbox would be a lot more effecient than re-running the geometry manager on one or more frames to add and remove whole widgets -- and could even seem more sensible from the user's perspective. Is there some compelling reason to be adding and destroying radio buttons in the main window while the user is watching?

        Thanks. I'd considered this option, and considering the expense of destroying the entire window I think I might opt for a listbox or drop menu type widget. The only thing in my favour is its an update that will rarely happen (only after admin changes).

        Dean
Re: TK - Updating Main Window after user input
by crabbdean (Pilgrim) on Jan 07, 2004 at 15:32 UTC
    Okay an update ... I've got it creating and adding in widgets dynamically. The second part to this saga is deleting objects after a users request they be taken out of the application. I having problems finding a widget destruction method. I know how to destroy Toplevels/Frames but Checkbutton widgets don't seem to like the destroy or withdraw methods.

    Anyone know what method is used to single destroy an object of a Frame? (I assume also afterwards you call "->update" on the Frame to update it).

    Dean
      I recommend you pick up "Mastering Perl Tk", the O'Reilly Emu Book. It's really one of the best GUI books I've ever read, and they make it really easy to follow. With a good reference, a lot of these questions can easily be answered. If you are going to be doing Tk a lot, see if you can buy it or get work to buy it for you.