Tk menu

B

Bart Masschelein

Hello there,

I started experimenting with Tk, out of the box, with the basic
examples, and I loved the way to implement menus:

menu_spec_1 =
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
['2 sub', 0]
]
]

and then calling

TkMenubar.new(root, menu_spec_2, 'tearoff'=>false).pack('fill'=>'x',
'side'=>'top')

However, I wanted to have a submenu under a menu item, so I tried

menu_spec_2 =
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
[ ['2 sub', 0],
['2 sub sub', 0]
]
]
]

This doesn't work, Ruby gives me an error. So apparently this is not
supported. Has sbdy already extended the menuspec.rb file to support
this behaviour, or would this straightforward to do, or even feasible
(I'm just starting with Ruby)?

I want to avoid the 'hassle' of creating each menu item itself, and then
structuring them. Been there, done that, in other languages.

happy Rubying,

Bart
 
J

Joe Van Dyk

Hello there,
=20
I started experimenting with Tk, out of the box, with the basic
examples, and I loved the way to implement menus:
=20
menu_spec_1 =3D
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
['2 sub', 0]
]
]
=20
and then calling
=20
TkMenubar.new(root, menu_spec_2, 'tearoff'=3D>false).pack('fill'=3D>'x',
'side'=3D>'top')
=20
However, I wanted to have a submenu under a menu item, so I tried
=20
menu_spec_2 =3D
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
[ ['2 sub', 0],
['2 sub sub', 0]
]
]
]
=20
This doesn't work, Ruby gives me an error. So apparently this is not
supported. Has sbdy already extended the menuspec.rb file to support
this behaviour, or would this straightforward to do, or even feasible
(I'm just starting with Ruby)?
=20
I want to avoid the 'hassle' of creating each menu item itself, and then
structuring them. Been there, done that, in other languages.

Yeah, the menu spec thing is sorta confusing for submenus. From the
TkMenuSpec docs (found at http://www.ruby-doc.org):

The format of the menu_spec is:

[ menu_info, menu_info, ... ]

And the format of the menu_info is:

[
[text, underline, configs], # menu button/entry (*1)
[label, command, underline, accelerator, configs], # command entry
[label, TkVar_obj, underline, accelerator, configs], # checkbutton ent=
ry
[label, [TkVar_obj, value],
underline, accelerator, configs], # radiobutton ent=
ry
[label, [[...menu_info...], [...menu_info...], ...],
underline, accelerator, configs], # cascade entry
'---', # separator
...
]

underline, accelerator, and configs are optional pearameters. Hashes
are OK instead of Arrays. Then the entry type ('command',
'checkbutton', 'radiobutton' or 'cascade') is given by 'type' key
(e.g. :type=3D>'cascade'). When type is 'cascade', an array of menu_info
is acceptable for 'menu' key (then, create sub-menu).
 
B

Bart Masschelein

I love this list!! The response time is almost like asking your
next-cubicle colleague! Anyway, if sbdy is still dazzled by how to do
submenu-ing, here is an example, using the previous reply:

menu_spec_2 =
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
['2 sub 1', [ ['2 sub sub 1', 0], ['2 sub sub 2', 0] ] ],
['2 sub 2', 0]
]
]

Thanks!

gr.b.
Hello there,

I started experimenting with Tk, out of the box, with the basic
examples, and I loved the way to implement menus:

menu_spec_1 =
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
['2 sub', 0]
]
]

and then calling

TkMenubar.new(root, menu_spec_2, 'tearoff'=>false).pack('fill'=>'x',
'side'=>'top')

However, I wanted to have a submenu under a menu item, so I tried

menu_spec_2 =
[ [ ['main 1', 0],
['1 sub', 0]
],
[ ['main 2', 0],
[ ['2 sub', 0],
['2 sub sub', 0]
]
]
]

This doesn't work, Ruby gives me an error. So apparently this is not
supported. Has sbdy already extended the menuspec.rb file to support
this behaviour, or would this straightforward to do, or even feasible
(I'm just starting with Ruby)?

I want to avoid the 'hassle' of creating each menu item itself, and then
structuring them. Been there, done that, in other languages.

Yeah, the menu spec thing is sorta confusing for submenus. From the
TkMenuSpec docs (found at http://www.ruby-doc.org):

The format of the menu_spec is:

[ menu_info, menu_info, ... ]

And the format of the menu_info is:

[
[text, underline, configs], # menu button/entry (*1)
[label, command, underline, accelerator, configs], # command entry
[label, TkVar_obj, underline, accelerator, configs], # checkbutton entry
[label, [TkVar_obj, value],
underline, accelerator, configs], # radiobutton entry
[label, [[...menu_info...], [...menu_info...], ...],
underline, accelerator, configs], # cascade entry
'---', # separator
...
]

underline, accelerator, and configs are optional pearameters. Hashes
are OK instead of Arrays. Then the entry type ('command',
'checkbutton', 'radiobutton' or 'cascade') is given by 'type' key
(e.g. :type=>'cascade'). When type is 'cascade', an array of menu_info
is acceptable for 'menu' key (then, create sub-menu).
 

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

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,504
Latest member
SherryFerr

Latest Threads

Top