# Trying to understand menus in ruby/tk through examples

**URL:** <https://www.ruby-forum.com/t/trying-to-understand-menus-in-ruby-tk-through-examples/70193>\
**Category:** Ruby\
**Created:** [September 27, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/trying-to-understand-menus-in-ruby-tk-through-examples/70193 "2006-09-27T13:07:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [September 27, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/trying-to-understand-menus-in-ruby-tk-through-examples/70193/1 "2006-09-27T13:07:27Z")

</div>

Can anyone explain why with the first two ‘command’ lines  
the program works. but with the third ‘command’ line it doesnt?

Im thinking im missing something in packing or definition?  
What Am I not getting?

require ‘tk’

root=TkRoot.new{title “NÃ­l aon tintÃ©an mar do thintÃ©an fÃ©in.”}  
canvas = TkCanvas.new.pack(‘fill’=\>‘both’, ‘expand’=\>‘true’)  
menuline = TkFrame.new(root, ‘borderwidth’=\>2)   
.place(‘anchor’=\>‘nw’, ‘relx’=\>0.0, ‘rely’=\>0.0,  
‘relwidth’=\>1.0)  
TkMenubutton.new(menuline) { |mb|  
text “File”  
underline 0  
menu TkMenu.new(mb) {  
add ‘command’,  
#label, variable, value, underline, command.  
‘label’ =\> ‘Save to DB’,  
‘underline’ =\> 0,  
‘command’ =\> proc { p “Save to database”; }  
add ‘separator’  
add ‘command’,  
‘label’ =\> ‘Load from DB’,  
‘underline’ =\> 0,  
‘command’ =\> proc { p “Load from database”; }  
add ‘command’  
‘label’ =\> ‘Exit program’,  
‘underline’ =\> 0,  
‘command’ =\> proc{exit}  
}  
pack(‘side’ =\> ‘left’, ‘padx’ =\> ‘1m’)  
}

canvas.pack  
#show gif on the canvas  
image = TkPhotoImage.new(‘file’ =\> ‘red\_pen.gif’)  
t=TkcImage.new(canvas, 100, 100,  
‘image’ =\> image)  
#end of show gif code.  
#code to place a label to show text  
TkLabel.new {  
text “This is a label”  
font TkFont.new(‘times 10 normal’)  
}.pack  
#end of code to show text  
canvas.focus

root.bind(‘Any-Key-s’, proc{|e| puts e.inspect; TkcRectangle.new(canvas,  
e.x,e.y,e.x-5, e.y-5)})

Tk.mainloop

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [September 27, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/trying-to-understand-menus-in-ruby-tk-through-examples/70193/2 "2006-09-27T17:41:20Z")

</div>

Mer G. wrote:

> Can anyone explain why with the first two ‘command’ lines  
> the program works. but with the third ‘command’ line it doesnt?

/ …

> ‘command’ =\> proc { p “Load from database”; }  
> add ‘command’  
> ‘label’ =\> ‘Exit program’,  
> ‘underline’ =\> 0,  
> ‘command’ =\> proc{exit}

There’s not enough code to create a working example, so this is just a  
guess, but I would ask where "exit’ is defined.

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [September 27, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/trying-to-understand-menus-in-ruby-tk-through-examples/70193/3 "2006-09-27T18:17:43Z")

</div>

Paul L. wrote:

> Mer G. wrote:
> 
> > Can anyone explain why with the first two ‘command’ lines  
> > the program works. but with the third ‘command’ line it doesnt?
> 
> / …
> 
> > ‘command’ =\> proc { p “Load from database”; }  
> > add ‘command’  
> > ‘label’ =\> ‘Exit program’,  
> > ‘underline’ =\> 0,  
> > ‘command’ =\> proc{exit}

> There’s not enough code to create a working example, so this is just a  
> guess, but I would ask where "exit’ is defined.

so the problem would be there?  
Ill check throught that tomorrow. Ill post it again if im still having  
problems.

---

<div class="post-metadata">

**Author:** ![merrua](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@merrua](https://www.ruby-forum.com/u/merrua)\
**Post date:** [September 27, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/trying-to-understand-menus-in-ruby-tk-through-examples/70193/4 "2006-09-27T21:25:53Z")

</div>

Mer G. wrote:

/ …

> > > ‘command’ =\> proc { p “Load from database”; }  
> > > add ‘command’  
> > > ‘label’ =\> ‘Exit program’,  
> > > ‘underline’ =\> 0,  
> > > ‘command’ =\> proc{exit}
> 
> > There’s not enough code to create a working example, so this is just a  
> > guess, but I would ask where "exit’ is defined.
> 
> so the problem would be there?

That depends. It depends on what you mean when you say it doesn’t work.  
Obviously if you call “exit” and “exit” has been redefined, or hangs for  
some reason, you will get the symptom that the application doesn’t exit.  
It  
might be a good idea to trace the action to see where it goes.
