Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Using Tk 8 menubar in its own window

by chrstphrchvz (Scribe)
on May 23, 2018 at 11:28 UTC ( [id://1215092]=perlquestion: print w/replies, xml ) Need Help??

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

Howdy, Monks!

Recently I've been trying to get a Perl/Tk program (HSW12, an IDE for programming microcontrollers) working with Tcl::pTk::TkHijack. One reason for doing so is letting the program be run on macOS using the native aqua UI instead of XQuartz. (Here's the full story on GitHub.)

One step I've taken is adapting the menubar to use the Tk 8 menu (cf. Mastering Perl/Tk - "12.2 Menubars and Pulldown Menus") rather than by packing Menubuttons. That way the native menubar at the top of the Mac's screen gets used instead of a menu in a window. However, the layout of the program has been such that the menubar is in its own window. When using the Tk 8 menu on non-Mac aqua, there is now a small amount of extra space (about 30 pixels vertically); and because there are no other widgets in the window, there doesn't appear to be anything to e.g. pack and remove the extra space. (See the bottom of this GitHub comment for a screenshot.)

Any ideas for how to remove this empty space/a way to force "pack" without widgets?

Some things I've already tried: I haven't found a suitable dummy widget that will shrink beyond some minimum size ≫0 (again, roughly 30 pixels). I thought I might set the toplevel's maxheight to the height of the menu, but I haven't found where that can be retrieved (the width and height of the menu is always 1x1 according to $menu->geometry). I'd rather not hardcode a height because the menu's height varies by platform and user theming/customizations.

Of course, one workaround would be to only use the Tk 8 menubar if running on Mac aqua (hiding the now-empty window using withdraw), and use the packed Menubuttons otherwise. One day I might use a module like Tk::IDElayout (compare to dock windows/panels in newer UI toolkits) which can let everything reside in a single toplevel window by default.

Replies are listed 'Best First'.
Re: Using Tk 8 menubar in its own window
by Anonymous Monk on May 23, 2018 at 11:42 UTC
    Its called a spacer

      Thanks for the quick reply. If by spacer you mean Frame, then your suggestion is reassuring in that I can use it with a simple test program:

      use warnings; use strict; use Tk 800.000; my $mw = MainWindow->new; my $menu = $mw->Menu(); my $file_menu = $menu->cascade(-label => 'File', -tearoff => 'false'); my $edit_menu = $menu->cascade(-label => 'Edit', -tearoff => 'false'); my $help_menu = $menu->cascade(-label => 'Help', -tearoff => 'false'); $mw->configure(-menu => $menu); my $spacer = $mw->Frame(-width => 200)->pack; # resize handle obscures Help menu $mw->resizable(0,0); MainLoop;

      I'm now inclined to think I didn't get this to work earlier because there's something else in the program that prevents a packed spacer from working as expected. I know the menu's window is being resized programmatically (so that it is stretched horizontally across the screen); I will have to keep digging to figure this out. I am not the program's original author, and am still figuring out my way around both it and Perl and Tk.

        It turns out the height of the menubar window was manually being set to 30 using $toplevel->geometry. Having it use height 0 instead eliminates the empty space. I then have to tell the program separately to pretend the menubar window has a height of 30 so that the positions calculated for the other windows do not overlap with the menubar window. Since I'm using geometry anyways, I can omit the spacer widget. Here's what that looks like for the test program:

        use warnings; use strict; use Tk 800.000; my $mw = MainWindow->new; my $menu = $mw->Menu(); my $file_menu = $menu->cascade(-label => 'File', -tearoff => 'false'); my $edit_menu = $menu->cascade(-label => 'Edit', -tearoff => 'false'); my $help_menu = $menu->cascade(-label => 'Help', -tearoff => 'false'); $mw->configure(-menu => $menu); # same effect as geometry('200x0+0+0') #my $spacer = $mw->Frame(-width => 200)->pack; $mw->geometry('200x0+0+0'); # resize handle obscures Help menu $mw->resizable(0,0); MainLoop;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1215092]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found