# Help with TkRoot

**URL:** <https://rubytalk.org/t/help-with-tkroot/30119>\
**Category:** ruby-talk\
**Created:** [19 August 2006 08:20 UTC](https://rubytalk.org/t/help-with-tkroot/30119 "2006-08-19T08:20:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_Virasoro](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@Diego\_Virasoro](https://rubytalk.org/u/Diego_Virasoro)\
**Post date:** [19 August 2006 08:20 UTC](https://rubytalk.org/t/help-with-tkroot/30119/1 "2006-08-19T08:20:15Z")

</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 Virasoro

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [19 August 2006 19:23 UTC](https://rubytalk.org/t/help-with-tkroot/30119/2 "2006-08-19T19:23:32Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;require 'tk'

&nbsp;&nbsp;&nbsp;&nbsp;root = TkRoot.new  
&nbsp;&nbsp;&nbsp;&nbsp;root.withdraw  
&nbsp;&nbsp;&nbsp;&nbsp;Tk.mainloop

Regards, Morton

> **···**
>
> On Aug 19, 2006, at 4:20 AM, Diego Virasoro wrote:
> 
> > 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?

---

<div class="post-metadata">

**Author:** ![Gregor\_Kopp](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gregor\_Kopp](https://rubytalk.org/u/Gregor_Kopp)\
**Post date:** [21 August 2006 06:30 UTC](https://rubytalk.org/t/help-with-tkroot/30119/3 "2006-08-21T06:30:08Z")

</div>

Diego Virasoro schrieb:

> 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 Virasoro

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.

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [19 August 2006 20:58 UTC](https://rubytalk.org/t/help-with-tkroot/30119/4 "2006-08-19T20:58:18Z")

</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:

\<code\>  
root = TkRoot.new {title 'Ruby Tk'}

mnu\_bar = TkMenu.new()  
&nbsp;&nbsp;&nbsp;&nbsp;windo\_mnu = TkMenu.new(mnu\_bar)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windo\_mnu.add\_command(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'=\>"Show",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'command'=\>lambda {windo\_mnu.show}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;windo\_mnu.add\_command(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'=\>"Hide",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'state'=\>'disabled',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'command'=\>lambda {windo\_mnu.hide}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def windo\_mnu.show  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entryconfigure(0, 'state'=\>'disabled') # disable show  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entryconfigure(1, 'state'=\>'normal') # enable hide  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tk.root.geometry($visible)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def windo\_mnu.hide  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entryconfigure(1, 'state'=\>'disabled') # disable hide  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entryconfigure(0, 'state'=\>'normal') # enable show  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tk.root.geometry("0x0+0+0")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;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  
\</code\>

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

> **···**
>
> On Aug 19, 2006, at 3:23 PM, Morton Goldberg wrote:
> 
> > Yes, you can make the root window invisible. The following produces a default menu bar but no (visible) root window on my Mac:
> > 
> > &nbsp;&nbsp;&nbsp;require 'tk'
> > 
> > &nbsp;&nbsp;&nbsp;root = TkRoot.new  
> > &nbsp;&nbsp;&nbsp;root.withdraw  
> > &nbsp;&nbsp;&nbsp;Tk.mainloop
