help with TkRoot

D

Diego Virasoro

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
 
M

Morton Goldberg

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
 
M

Morton Goldberg

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()
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
</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
 
G

Gregor Kopp

Diego said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top