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

Hi Monks,

I need help from you guys...

I created a window with two radio buttons and one OK button.

The number of radio buttons to be created(currently it is 2) is determined by a variable.

If that variable is 5, i have to create 5 radiobuttons and then OK button.

My problem is ,if i am creating 20 radio buttons, my mainwindow should automatically resize and if i want to create only 1 radio button,main window should automatically reduce its size...Is there any option for that?????

Thanks, ch123

Replies are listed 'Best First'.
Re: How to resize mainwindow automatically
by moritz (Cardinal) on Oct 26, 2009 at 12:26 UTC

    What GUI toolkit are you using?

    I was under the impression that Tk does that by default, at least if you use the pack geometry managers. (Though it's been some time since I last use Tk, so I might be wrong).

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: How to resize mainwindow automatically
by biohisham (Priest) on Oct 26, 2009 at 12:32 UTC
    Where is the code you created this window with? and what module are you using? and you said "Is there any option for that?????", I wanna know, what have you tried doing so far?

    You can try to boil down your problem to a brief representation that replicates the situation you are facing and we all would cooperate on it, but you have to show you've tried to be independent...

    Update: moritz is right, I've not seen a need to provide for widgets containment within a MainWindow, since it can automatically take care of its size shrinking and expanding to accumulate the widgets you feed it.


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

      I am using perl\Tk... As of I know for a top-level window , we cant use pack.. Here is the code..which i have written...

      # Defines the Main Frame in the Main Window $frame1 = $mainwindow ->Frame(-relief => 'flat', -borderwidth => $fram +e_borderwidth, -background => $mainframes_background , -foreground => $frameforeground +, -width => 475, -height => 320) ->pack(-padx => 10, -pady => 19, -ipadx +=> 80, -ipady => 70);
      # Defining the Fuel and Oil Flow Rates - Instrumentation Label $mainwindow ->Label(-text => " Which $Fuel_Type tank are you using + ? ", -background => "DarkBlue", -borderwidth => 0, -foreground => $mainframes_background, -relief => 'gro +ove', -font => [$style , $size, $visibility]) ->pack() ->place(-x => 100, -y => 11);
      # Spacer between the frame 1 border and frame2 border $spacer1 = $frame1 ->Frame(-relief =>'flat', -borderwidth => $frame +_borderwidth, -background => $mainframes_background, -foreground +=> $frameforeground, -height => 10) ->pack(-padx => 8, -pady => 0, -ipadx => 0, -ipady => 0 +, -fill => 'x', -side => 'top');
      # Creating the frame to hold the controls $frame2 = $frame1->Frame(-relief => 'flat', -borderwidth => 3, + -background => $mainframes_background, -foreground = +> $frameforeground) ->pack(-padx => 8, -pady => 0, -ipadx => 0, -ipady => 0, -fill => 'x', -side => ' +top');
      for($i = 1;$i <= $nTanks;$i++){
      $Rb[$i-1] = $frame2 -> Radiobutton(-text => $i,-value => $i,-variable +=> \$Tank_Num, -width => 80, -anchor => 'w', +-background => $mainframes_background, -font => [$style , $size, $vi +sibility]); <code>$Rb[$i-1] -> grid($Rb[$i-1],-sticky => 'w',-padx => 10,-pady => +3); }
       $button = $frame2 -> Button(-text => "  OK  ", -font=>[$style, $size, $visibility], -command => \&ok_callback); $button -> grid($button,-sticky => 'w',-padx => 10,-pady => 8);
        if you would be kind enough to put the entire runnable code example into a single code block, i might be able to help you...... at first glance you mix grid and pack...... i don't think it is wise to mix layout managers

        ....as far as auto-resizing goes, resizing the mainwindow is usually a bad option..... what you should do, is pack your buttons in a Scrolled Pane, and let the scrolled pane adjust the size of the scroll region to accomodate extra buttons......or look at the following

        #!/usr/bin/perl use strict; use Tk; my $mw = tkinit; my %cb; my $f = $mw->Frame->pack(-anchor => 'w'); foreach (qw/Author Reviewer File Status Mine/) { $cb{$_} = $f->Checkbutton(-text => $_)->pack(-anchor => 'w'); } my $on_off = 1; foreach (['Enable', 1], ['Disable', 0]) { $mw->Radiobutton( -text => $_->[0], -command => [\&ChangeAuthor, $_->[1]], -variable => \$on_off, -value => $_->[1], )->pack(-side => 'left'); } MainLoop; sub ChangeAuthor { if (shift) { $cb{Author}->pack(-before => $cb{Reviewer}, -anchor => 'w'); } else { $cb{Author}->packForget; } }

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
        I am using perl\Tk... As of I know for a top-level window , we cant use pack..

        I am curious what would give you that idea? pack() is the most used (far and away) geometry manager. I see nothing in your application that would require "grid". My advice: Learn how to use pack() before trying one of the other geometry managers.

        One very common problem is not using enough frames. You seem to get the idea that frames can be packed inside of other frames. This is VERY common and the right way of using pack(). In my experience things like "place(-x => 100, -y => 11);", using "fixed" x,y coordinates is a bad idea, especially with some dynamic geometry things like varying the number of widgets that will be displayed.

        If you want say 2 columns, make a frame for the columns, then a frame for col 1 and col 2. Pack widgets into say the left side of the column frames (also could be right or center). In this case -padx, -pady are good things to know about as well as fill.

        I have found that the most helpful thing is to draw a simple diagram on paper of what you are trying to do, showing the frames and frames within frames, etc. All windows are pretty much the same except that a mainwindow will always exist (closing mainwindow exits Tk). You can hide or iconify the main windows, but it will still "exist".

        I think you have a mis-understanding of what geometry() does. This IS the minimum window size! If you do the frame packing right, the window will expand. Awhile ago, I wrote a simple program to demo to show that the main window will grow at Re: Perl Tk geometry problem. Play with that simple code and you will see that you can't make the window smaller than this geometry value. I don't know of anything like "maxsize".

        If the stuff to get display won't fit on screen, then as another poster suggested, scrolled is a good method. But I don't think that you need this.

        I think you are getting warnings that you aren't seeing. I echo comments about "use strict; use warnings;".

        My suggestion would be to come back with some code using only the standard geometry manager (pack). Make this as simple as you can to demonstrate the problems.

        It is not clear to me whether you need to change this mainwindow set of buttons dynamically during course of program execution. You could clarify that requirement. Anyway the first step is to get some stuff on the screen like you want. Get that working. Then 2nd question is how to "re-do" this window with more or fewer buttons during course of program execution.

        Update: I wrote some simple code for you to get started..

        Main points:
        - show some code that runs with "use warnings; use strict;" - Take out fancy formatting options, like "-relief" although I showed this above. Your objective should be to show a simple example that demonstrates the problem.
        - use some kind of consistent indenting style to make your code easy to read.
        I FORGOT TO ATTATCH THESE LINES OF CODE AT THE BEGINNING...SORRY FOR THE INCONVENIENCE
        # Creating the Main Window $mainwindow = MainWindow->new(-title => " Select Fuel Tank for $Fu +el_Type Fuel ");
        $mainwindow->configure( -background=> $mainframes_background);
        # Defines the Geometry for the Main Window $mainwindow->geometry("400x300"); $mainwindow->maxsize(400,500; $mainwindow->minsize(400,300);
Re: How to resize mainwindow automatically
by Anonymous Monk on Oct 26, 2009 at 12:27 UTC
    packforget/pack