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

Please forgive me that I have not done my research on this one. Just tell me if there is a simple way to set the height in _screen pixels_ of an Optionmenu exactly the same as a Button. This is necessary for me because I regularly have Buttons and Optionmenus in the same row side by side on my control panels and the Optionmenu being a few pixels higher is very disturbing. I know there is -pady for the Button but how to determine the exact offset in pixels?
  • Comment on Make Tk Optionmenu same height as Button

Replies are listed 'Best First'.
Re: Make Tk Optionmenu same height as Button
by kcott (Archbishop) on Jan 25, 2012 at 07:19 UTC

    The -pady option is a standard widget option, see Tk::options.

    In addition, there are options -pady and -ipady for the geometry managers pack() and grid(). If you are using a different geometry manager, check the appropriate documentation - you should find a link to it on http://search.cpan.org/~srezic/Tk-804.030/.

    There's an excellent discussion of geometry managers in the O'Reilly book Mastering Perl/Tk - the whole of chapter 2 is devoted to this subject.

    I suspect using -ipady with pack() will probably do what you want but, as you've posted no code, I can only guess.

    -- Ken

Re: Make Tk Optionmenu same height as Button
by BrowserUk (Patriarch) on Jan 25, 2012 at 02:52 UTC

    This works for me using the default fonts:

    #! perl -slw use strict; use Tk; my $mw = MainWindow->new(); my $om = $mw->Optionmenu( -options => [ [fred => 1], [bill => 2] ] )->pack( -side=> 'left' ); my @btns = map { $mw->Button( -text => $_, -pady => 2 )->pack( -side=> 'left' ); } 1 .. 8; MainLoop;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Sure it may be platform dependent.

      By the way what is this 'map' thing? Is this a relatively new function in perl or am I under educated?
        Sure it may be platform dependent.

        I cannot prove it without seeing your code, but I don't think so.

        The -pady values (without suffixes) are in pixels. The symptoms you describe (1 too small; 2 too big), are indicative that the y padding is being added both above and below. And that is indicative that the button is being packed with the default -anchor value which is centre.

        You should find that if you adjust the packing so that it is no longer centered, the -pady will only be added to one edge, allowing you to match it to the Optionmenu size with -pady =>1.

        By the way what is this 'map' thing?

        See map.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        By the way what is this 'map' thing? Is this a relatively new function in perl or am I under educated?

        Map has been there since 1994

Re: Make Tk Optionmenu same height as Button
by Marshall (Canon) on Jan 25, 2012 at 01:45 UTC
    In my experience, using -pady, -padx doesn't work out that well.

    In this case, the MenuButton idea is a great one as I think it is the same size as other buttons.

    The more general advice is to use more sub-frames and the various anchor and centering options to get things to "line up". I draw "pencil on paper" diagrams before attempting to write a Tk GUI. This helps a lot! Draw diagrams showing the frames. Think sub-frame to get things to line up. Stay away from pixel oriented stuff.

    Different users will have different frame resolutions and use say "large fonts" instead of "small fonts" - basically you are "doomed" if you deal with pixels. If you have a frame and pack in 2 widgets, that are are packed in left to right and then "centered" within that frame, then this work (in my experience).

    GUI coding is one area where "mileage varies a lot!". But I think the basic advice of thinking "sub-frames" will take you a very long way down the road.

      This prog is for my own use. Portability is not an issue for me. In my case -pady=>2 creates a Button smaller, -pady=>3 a Button bigger than an Optionmenu :). So this lazy unportable trial and error solution does not work for me. Now I may have to learn creating menus. At first sight it looks much more complicated (how to reproduce Optionmenu functionality with a Menubutton?). Also you seem to be limited to a single menu bar / main window (at the top). I have several control panels in my main window scattered all around the place.

      Your sub-frame approach I fully appreciate. From my experience I have learned to put everything in void container frames (even in separate Toplevel windows which can be iconified). Every functional module in my prog gets a container frame to start with in the constructor which it can populate but knows nothing else about the world.
Apology for Tk Optionmenu vs. Button height
by chessgui (Scribe) on Jan 25, 2012 at 10:58 UTC
    Guys, who contributed to the 'Make Tk Optionmenu same height as Button' thread: I apologize for my comments.

    I'have looked at it again and again and I have to admit that it is probably true that at some value for -pady the y dimensions of those widgets are exactly the same. The type of the geometry manager makes no differnce in that regard. It may be the construction of the Optionmenu (which has more space inside it) which leads me to some strange optical illusion that it is not the same size as the Button.

Re: Make Tk Optionmenu same height as Button
by Anonymous Monk on Jan 24, 2012 at 22:23 UTC
    use a menubutton, see widget demo for example