# Tk TopLevel window

**URL:** <https://www.ruby-forum.com/t/tk-toplevel-window/108230>\
**Category:** Ruby\
**Created:** [June 30, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/tk-toplevel-window/108230 "2007-06-30T16:58:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [June 30, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/tk-toplevel-window/108230/1 "2007-06-30T16:58:18Z")

</div>

I have been searching for awhile but cannot find the answer to my  
question.  
I have a TkRoot window that when a button is clicked, a new TkToplevel  
window is created.

I don’t want the user to be able to get focus of the TkRoot window until  
the TkToplevel window is closed. Much like a dialog box. I could  
probably catch the FocusOut event on the TkToplevel window and grab  
focus again but that seems like a hacked way to do it. Is there an  
easier way?

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [July 2, 2007, 4:06am UTC](https://www.ruby-forum.com/t/tk-toplevel-window/108230/2 "2007-07-02T04:06:39Z")

</div>

From: Jason [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Tk TopLevel window  
Date: Sat, 30 Jun 2007 23:58:33 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I have a TkRoot window that when a button is clicked, a new TkToplevel  
> window is created.
> 
> I don’t want the user to be able to get focus of the TkRoot window until  
> the TkToplevel window is closed. Much like a dialog box. I could  
> probably catch the FocusOut event on the TkToplevel window and grab  
> focus again but that seems like a hacked way to do it. Is there an  
> easier way?

Please use TkWindow#set\_grab/release\_grab methods,  
and some kinds of tkwait methods.

# See also Tcl/Tk’s manuals about ‘tkwait’ and ‘vwait’.

## For example,

require ‘tk’

def create\_onetime\_toplevel(btn)  
top = TkToplevel.new(btn).withdraw  
TkLabel.new(top, :text=\>'self.object\_id == ’ \<\<  
top.object\_id.to\_s).pack  
e = TkEntry.new(top).pack  
TkButton.new(top, :text=\>‘CLOSE’, :command=\>proc{top.destroy}).pack  
top.deiconify  
e.focus  
top.set\_grab  
top.wait\_destroy # same as top.tkwait\_destroy  
top.release\_grab  
end

def create\_reusable\_toplevel(btn, var)  
top = TkToplevel.new(btn).withdraw  
TkLabel.new(top, :text=\>'self.object\_id == ’ \<\<  
top.object\_id.to\_s).pack  
e = TkEntry.new(top).pack  
TkButton.new(top, :text=\>‘CLOSE’,  
:command=\>proc{top.withdraw; var.value = 1}).pack  
[top, e]  
end

root = Tk.root  
TkEntry.new.pack.focus  
TkButton.new(:text=\>‘QUIT’, :command=\>proc{exit}).pack(:side=\>:bottom)

b1 = TkButton.new(:text=\>‘onetime toplevel’,  
:command=\>proc{create\_onetime\_toplevel(b1)}).pack

b2 = TkButton.new(:text=\>‘reusable toplevel’).pack  
wait\_var = TkVariable.new

reusable\_top, entry = create\_reusable\_toplevel(b2, wait\_var)

b2.command{  
reusable\_top.deiconify  
entry.focus  
reusable\_top.set\_grab  
wait\_var.tkwait # wait for writing to wait\_var.  
# It’s similar to wait\_var.wait,  
# but tkwait checks the existence of Tk’s root  
window.  
# When the root window is destroyed, tkwait will  
finish.  
reusable\_top.release\_grab  
reusable\_top.withdraw  
}

Tk.mainloop
