# Help with TkRoot

**URL:** <https://www.ruby-forum.com/t/help-with-tkroot/65567>\
**Category:** Ruby\
**Created:** [August 19, 2006, 10:22am UTC](https://www.ruby-forum.com/t/help-with-tkroot/65567 "2006-08-19T10:22:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [August 19, 2006, 10:22am UTC](https://www.ruby-forum.com/t/help-with-tkroot/65567/1 "2006-08-19T10:22:27Z")

</div>

Hello,  
I would like to write a program on the Mac which at the start only  
shows the menubar: only after the user has selected an option from the  
menu would the first window appear.

However I can’t get that to work in Tk. It seems that a Tk GUI can’t do  
without a TkRoot widget and TkRoot is shown as a window.

Is there any way to make the TkRoot be the menubar itself? Or maybe to  
make TkRoot invisible? Or any other solution to achieve the same goal?

Thank you.

Diego V.

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [August 19, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/help-with-tkroot/65567/2 "2006-08-19T21:25:30Z")

</div>

Yes, you can make the root window invisible. The following produces a  
default menu bar but no (visible) root window on my Mac:

```
require 'tk'

root = TkRoot.new
root.withdraw
Tk.mainloop

```

Regards, Morton

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [August 19, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/help-with-tkroot/65567/3 "2006-08-19T22:59:39Z")

</div>

Now that I’ve looked a little deeper into the problem, I see that  
Tk.root.withdraw wouldn’t be useful, because in your case, if you use  
it, you won’t see the root window’s menu bar. What to do? Well, you  
might try something like the following:

`
root = TkRoot.new {title 'Ruby Tk'}
mnu_bar = TkMenu.new()
windo_mnu = TkMenu.new(mnu_bar)
windo_mnu.add_command(
‘label’=>“Show”,
‘command’=>lambda {windo_mnu.show}
)
windo_mnu.add_command(
‘label’=>“Hide”,
‘state’=>‘disabled’,
‘command’=>lambda {windo_mnu.hide}
)
def windo_mnu.show
entryconfigure(0, ‘state’=>‘disabled’) # disable show
entryconfigure(1, ‘state’=>‘normal’) # enable hide
Tk.root.geometry($visible)
end
def windo_mnu.hide
entryconfigure(1, ‘state’=>‘disabled’) # disable hide
entryconfigure(0, ‘state’=>‘normal’) # enable show
Tk.root.geometry(“0x0+0+0”)
end
mnu_bar.add_cascade(‘label’=>“Window”, ‘menu’=>windo_mnu)
root.menu(mnu_bar)
min_w, min_h = 200, 100
root.minsize(min_w, min_h)
root_x = (root.winfo_screenwidth - min_w) / 2
$visible = “#{min_w}x#{min_h}+#{root_x}+50”
root.geometry(“0x0+0+0”)
root.bind(‘Command-q’) {exit}
Tk.mainloop
`

This is my best shot at the moment. It’s not very elegant since you  
can still see the vestigial root window in the upper left-hand corner  
of the screen if you look hard, but perhaps it’s good enough to fool  
your users 🙂

Regards, Morton

---

<div class="post-metadata">

**Author:** ![diego\_v](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@diego\_v](https://www.ruby-forum.com/u/diego_v)\
**Post date:** [August 21, 2006, 8:32am UTC](https://www.ruby-forum.com/t/help-with-tkroot/65567/4 "2006-08-21T08:32:27Z")

</div>

Diego V. schrieb:

> Thank you.
> 
> Diego V.

Maybe you could try to define the menubar in the root window with proper  
size and then, if the entry in the menu is selected you could make a  
toplevel.
