# Problem with TkMenu::add\_cascade

**URL:** <https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997>\
**Category:** Ruby\
**Created:** [September 26, 2006, 9:13am UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997 "2006-09-26T09:13:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 26, 2006, 9:13am UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997/1 "2006-09-26T09:13:45Z")

</div>

Hi list,

i’m working on gui using ruby/tk.  
Everything went fine (exept the poor documentation), but now i have a  
problem with submenus. A small example-code to demonstrate the issue:

menu = TkMenu.new  
menu.add\_command(‘label’ =\> ‘Foo’, ‘command’ =\> lambda { p “foo” })  
sub = TkMenu.new  
sub.add\_command(‘label’ =\> ‘Bar’, ‘command’ =\> lambda { p “bar” })  
menu.add\_cascade(‘label’ =\> ‘submenu’, ‘menu’ =\> sub)

canvas = TkCanvas.new  
canvas.bind(‘1’) {|e| menu.popup e.x\_root, e.y\_root}  
canvas.pack

Tk.mainloop

The code works nearly as expected, menu appears an has correct content.  
If i click on “Foo”, text “foo” is written to the console, but if i  
click on  
“Bar” nothing happens ☹  
Any idea what’s going wrong?

Ruby: ruby 1.8.5 (2006-08-25) [i486-linux]  
Linux: Linux version 2.6.12 (root@louipaz) (gcc version 3.3.6 (Debian  
1:3.3.6-13))

thanks a lot,  
Matthias

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 26, 2006, 11:10am UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997/2 "2006-09-26T11:10:18Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi list,
> 
> i’m working on gui using ruby/tk.

Any reason you aren’t using Qt? I don’t want to sound like a broken  
record  
here, but Qt’s documentation is much better than for Tk, the apps look  
better, and so forth.

> canvas.bind(‘1’) {|e| menu.popup e.x\_root, e.y\_root}  
> canvas.pack
> 
> Tk.mainloop

How about a complete example, with headers, so we can test the code?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 26, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997/3 "2006-09-26T12:44:32Z")

</div>

removed\_email\_address@domain.invalid skrev:

> menu.add\_cascade(‘label’ =\> ‘submenu’, ‘menu’ =\> sub)  
> “Bar” nothing happens ☹  
> Any idea what’s going wrong?
> 
> Ruby: ruby 1.8.5 (2006-08-25) [i486-linux]  
> Linux: Linux version 2.6.12 (root@louipaz) (gcc version 3.3.6 (Debian  
> 1:3.3.6-13))
> 
> thanks a lot,  
> Matthias  
> Tested your code (just added require ‘tk’ as first line) with  
> Ruby 1.8.5 on WinXP and Active State Tcl 8.4.13.

No problem, works fine, both “foo” and “bar” printed as  
they should. Maybe your Tcl needs updating?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 26, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997/4 "2006-09-26T15:54:55Z")

</div>

On Sep 26, 2006, at 3:12 AM, removed\_email\_address@domain.invalid wrote:

> menu.add\_cascade(‘label’ =\> ‘submenu’, ‘menu’ =\> sub)  
> click on  
> “Bar” nothing happens ☹  
> Any idea what’s going wrong?
> 
> Ruby: ruby 1.8.5 (2006-08-25) [i486-linux]  
> Linux: Linux version 2.6.12 (root@louipaz) (gcc version 3.3.6 (Debian  
> 1:3.3.6-13))

No problem with your code on my box – Mac OS X 10.4, ruby 1.8.2.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 26, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997/5 "2006-09-26T20:23:48Z")

</div>

From: removed\_email\_address@domain.invalid  
Subject: Problem with TkMenu::add\_cascade  
Date: Tue, 26 Sep 2006 16:12:32 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> The code works nearly as expected, menu appears an has correct content.  
> If i click on “Foo”, text “foo” is written to the console, but if i  
> click on  
> “Bar” nothing happens ☹  
> Any idea what’s going wrong?
> 
> Ruby: ruby 1.8.5 (2006-08-25) [i486-linux]  
> Linux: Linux version 2.6.12 (root@louipaz) (gcc version 3.3.6 (Debian  
> 1:3.3.6-13))

That is NOT a bug of Ruby/Tk. Tcl/Tk will give you a same result.  
Probably, your trouble depends on the relationship between the  
menu and the cascade menu.  
Please try to make the cascade menu as a daughter of the menu  
(e.g. “sub = TkMenu.new(menu)”).  
^^^^

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 27, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/problem-with-tkmenu-add-cascade/69997/6 "2006-09-27T12:14:32Z")

</div>

> That is NOT a bug of Ruby/Tk. Tcl/Tk will give you a same result.  
> Probably, your trouble depends on the relationship between the  
> menu and the cascade menu.  
> Please try to make the cascade menu as a daughter of the menu  
> (e.g. “sub = TkMenu.new(menu)”).  
> ^^^^

That’s it, thanks a lot!

One thing i noticed while testing:  
it also depends on the windowmanager. On KDE (for example) the “bad”  
code works fine, on fluxbox it doesn’t…

best regards,  
Matthias
