0
menubutton .mb -text Example -menu .mb.menu
pack .mb -padx 10 -pady 10

set m [menu .mb.menu -tearoff 1]
$m add cascade -label A -menu $m.sub1
$m add cascade -label B -menu $m.sub1
$m add cascade -label C -menu $m.sub1

set m2 [menu $m.sub1 -tearoff 0] 
$m2 add radio -label x -variable fruit -value apple
$m2 add radio -label y -variable fruit -value orange
$m2 add radio -label z -variable fruit -value kiwi 

Lets say someone click on B then on z, i want to print Bz. Lets say someone click on A then on y, i want to print Ay.

How to do that? i.e. it should pass menu values to submenu values

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215

1 Answers1

0

When you use a single menu in multiple cascade entries like that, you cannot tell them apart. To distinguish them, you should create the submenu three times and either bind the callbacks through (as below) or give each one its own -postcommand.

menubutton .mb -text Example -menu .mb.menu
pack .mb -padx 10 -pady 10

set m [menu .mb.menu -tearoff 1]
foreach cmd {sub1 sub2 sub3} tag {A B C} {
    set m2 [menu $m.$cmd -tearoff 0]
    foreach label {x y z} value {apple orange kiwi} {
        $m2 add radio -label $label -variable fruit -value $value \
                -command [list puts $tag$label]
    }

    # I prefer to add the cascades after making the submenu
    $m add cascade -label $tag -menu $m2
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • I investigated whether `cascade`s get `invoke`d so that their `-command` would do anything useful… but they don't. The `-postcommand` on the submenus would work… but then you've already got individual menus and might as well go with direct binding. – Donal Fellows Nov 19 '20 at 11:55