Version 1 of m+

Updated 2006-08-24 08:46:40 by suchenwi

Richard Suchenwirth 2006-08-24 - Specifying menu items can get a bit verbose due to several keywords that have to be used, but since TkPhotoLab I've started to use this little wrapper which concentrates the ugly reality in one place, and henceforth allows to use beautifully simple menu specifications:

 proc m+ {head name {cmd ""}} { 
    if {![winfo exists .m.m$head]} { 
         .m add cascade -label $head -menu [menu .m.m$head -tearoff 0] 
    } 
    if [regexp ^-+$ $name] { 
            .m.m$head add separator 
    } else {.m.m$head add command -label $name -comm $cmd} 
 }

#-- Demo example:

 #!/usr/bin/env tclsh 
 package require Tk 

 . configure -menu [menu .m] 
 m+ File Open {.t insert end "opened\n"} 
 m+ File Save {.t insert end "saved\n"} 
 m+ File ----- 
 m+ File Exit exit 

 m+ Edit Cut ... 

 pack [text .t -wrap word] -fill both -expand 1

An earlier, less simple approach is at Menus made easy


Category GUI | Arts and crafts of Tcl-Tk programming