How to remove a menu bar from a frame?

For creating a menu bar, the documentation says:

new menu-bar% - A menu-bar% object is created for a particular frame% object. A frame can have at most one menu bar; an exn:fail:contract exception is raised when a new menu bar is created for a frame that already has a menu bar.

I can create a menu bar with this code:

#lang racket/gui
(define frame (new frame% [label "Racket Frame"] [width 200] [height 200]))
(define menu-bar (new menu-bar% [parent frame]))
(define menu (new menu% [label "File"] [parent menu-bar]))
(send frame show #t)

After it has been created, how can I remove the menu bar from the frame?

Hi, @cadence.

I had a quick look at the source for the menu-bar% and frame%. I am just spit-balling here, so take with a grain of salt, but I think the intended "workflow" if you will, is to have a single menu bar, which is either visible or not, which has items which can delete themselves. So, the menu bar itself stays constant, but its content varies.

I hope that helps.

1 Like

I see. So I can (send menu delete) and (send menu restore) to hide/show. That works to clear the items, but the blank space remains there at the top of the window. I guess that's good enough.

My workaround was to delete and re-create the entire window. This works well enough for me, but one notable downside is if it's been moved around the screen by the user, it'll reappear in the middle.

I see. That is a pickle.

Maybe you can "hide" the menu bar with (send menu-bar enable #false), which will disable it. But I don't know if that has the intended effect from just one test.



I meant that the menu bar will still reserve space in the window even if it is hidden/deleted. Try this larger example:

#lang racket/gui
(define frame (new frame% [label "Racket Frame"] [width 200] [height 200]))
(define check (new check-box%
                   [parent frame]
                   [label "Check Box"]
                   [value #t]))
(send frame show #t)

(define menu-bar (box #f))
(define menu (box #f))

(define (create-menu)
  (set-box! menu-bar (new menu-bar% [parent frame]))
  (set-box! menu (new menu% [label "File"] [parent (unbox menu-bar)])))

(define (disable-delete-menu)
  (send (unbox menu-bar) enable #f)
  (send (unbox menu) delete))

At first the menu bar doesn't exist, so the checkbox is right next to the top of the window.

Then, after running (create-menu) in the REPL, the menu bar is added to the top of the window and pushes the checkbox down a bit.

Finally, after running (disable-delete-menu) in the REPL, the menu bar disappears but it isn't truly gone. It leaves behind a blank space at the top of the window, stopping the checkbox from going back to the top of the window.

Perhaps it is a platform-related issue, because I cannot recreate what you describe, although I understand what you mean.




Edit: I am running a Windows 10 Professional edition, to clarify the point.

1 Like

Oh, you're totally right! It does remove the menu entirely on Windows. On Linux I get the blank space left behind. Here's what it looks like for me:

1 Like

I wonder how you would troubleshoot the issue :thinking:

Does it continue if you add a new item to the frame? Or perhaps, if you are using a tiling WM, move the window around?

Edit: I am alluding to whether this is a platform-related issue, or merely a "refresh" issue.

Yes. I think it's a platform issue rather than a refresh issue.

1 Like

With Gtk, I think racket/gui reserves that menu-bar space here:

It's possible that some configuration could reset that space back to 0, although I'm not sure it would work right.

1 Like