ttk::menubutton

Ttk's menubutton widget.

https://www.tcl-lang.org/man/tcl8.5/TkCmd/ttk_menubutton.htm

jnc To invoke a menu button (or almost any other widget that takes a -text and -underline option) by a keystroke you can:

  event generate $widget <<Invoke>>

Such as:

  bind <Alt-f> . {event generate .menuButton <<Invoke>>}

 Alignment question

HaO 2010-11-16 Is there any possibility to center-align the menu button text and image ? This would make the following example nicer (on Windows Vista classic theme):

https://wiki.tcl-lang.org/_repo/images/align_toolbuttons.png

("Laden" has nothing to do with the person, it is German for "Load (Module in this case)")

Labels and Images are left-aligned.

Code fragment (not that of the screenshot):

% pack [ttk::menubutton .t1 -text abcabcabc] -fill x
% pack [ttk::menubutton .t2 -text a] -fill x

Style investigation:

% ttk::style layout TMenubutton
Menubutton.border -sticky nswe -children {Menubutton.focus -sticky
nswe -children {Menubutton.indicator -side right -sticky {}
Menubutton.padding -expand 1 -sticky we -children {Menubutton.label -
side left -sticky {}}}}

I think, the issue is the last part. "-side left -sticky {}" which should be changed to "-sticky news" Is this possible in an easy manner ?

--- Even harder, I want to have it with Toolbutton style:

% pack [ttk::menubutton .t1 -text abcabcabc -style Toolbutton] -fill x
% pack [ttk::menubutton .t2 -text a -style Toolbutton] -fill x

How to align the labels "centered ?"

--- Style investigation:

% ttk::style layout Toolbutton
Toolbutton.border -sticky nswe -children {Toolbutton.padding -sticky
nswe -children {Toolbutton.label -sticky nswe}}

Here, the label has -sticky "nswe". So try some label-options:

% ttk::style configure Toolbutton.label -justify center -anchor center
% pack [ttk::menubutton .t3 -text a -style Toolbutton] -fill x

But stil left-aligned...

But

% ttk::style theme use clam

and the Toolbutton styled menubuttons are center-aligned.

Why is a normal Button centered and a MenuButton left-aligned ?

If you have an answer feel free to remove all this and replace it by more helpful stuff. I have posted this on c.l.t. with no answer and solved it by putting the label to the right of the icon so it looks pretty when left-aligned.


EG: Remove the "-side left" and add "-sticky ns".

For plain ttk::menubuttons:

% ttk::style layout TMenubutton {
  Menubutton.border -sticky nswe -children {
    Menubutton.focus -sticky nswe -children {
      Menubutton.indicator -side right -sticky {}
      Menubutton.padding -expand 1 -sticky we -children {
        Menubutton.label -sticky ns
      }
    }
  }
}

and for the Toolbutton styled ones:

% ttk::style layout Toolbutton {
  Toolbutton.border -sticky nswe -children {
    Toolbutton.padding -sticky nswe -children {
      Toolbutton.label -sticky ns
    }
  }
}


  Post also with right mouse-click

HaO 2011-05-10 To post the menu also on the right mouseclick, one may generate the left events also on the right click events:

bind TMenubutton <ButtonPress-3>          { event generate %W <ButtonPress-1> }
bind TMenubutton <ButtonRelease-3>        { event generate %W <ButtonRelease-1> }

alternate solution not depending on Button 1 any more:

bind TMenubutton <ButtonPress-3>          [bind TMenubutton <ButtonPress-1>]
bind TMenubutton <ButtonRelease-3>        [bind TMenubutton <ButtonRelease-1>]


  Open issue: post by mouse-over

HaO 2011-05-10 I tried to post the menu if the mouse is over the menubutton.

There is no issue to post the menu when the mouse is over by (code derivated from tk8.5.9/lib/tk8.5/ttk/menubutton.tcl):

if {[tk windowingsystem] eq "x11"} {
    bind TMenubutton <Enter>          { ttk::menubutton::Pulldown %W }
    bind TMenubutton <Leave>        { ttk::menubutton::TransferGrab %W }
} else {
    bind TMenubutton <Enter>  \
        { %W instate !disabled {%W state active ;\
        %W state pressed ;\
        ttk::menubutton::Popdown %W }}
    bind TMenubutton <Leave>  \
        { %W state !active ; %W state !pressed }
}

Windows issues: The menu is well posted and gets a local grab which may only be unposted by clicking outside the menu.

With two menubuttons, the following happens:

  • A menu is posted when the first Menubutton gets focus
  • When the second gets focus, its menu is not posted, because the first still owns the grab

The following comment from the original tk8.5.9 menubutton.tcl file suggestes, that this is not easy to solve:

# I'm not sure what the hell is going on here.  [$menu post] apparently 
# sets up some kind of internal grab for native menus.


  Get themed button colors

Martyn Smith asked on clt and Koen answered:

All I need to find are the 4 colours needed to set on the menu I was not hoping to have themed menus just the same colour scheme. There does not seem to be a 'Theme independant' way to find fg, bg, activefg and activebg.

What did you try? Doesn't the following work?

% ttk::style theme use clam
% ttk::style lookup TMenubutton -background
#dcdad5
% ttk::style lookup TMenubutton -foreground
black
% ttk::style lookup TMenubutton -background active
#eeebe7
% ttk::style lookup TMenubutton -foreground active
black 


  Bind a Control-Drag operation

HaO 2011-07-07 Within a local drag and drop, one may define a drag with the control button by the following events:

# > To avoid the nocontrol events to fire when control is pressed,
# > define some dummy nocontrol-events.
bind $WiButton <Control-ButtonPress-1> break
bind $WiButton <Control-ButtonRelease-1> break

bind $WiButton <Control-B1-Motion> "Drag %W %X %Y"
bind $WiButton <Control-ButtonRelease-1> "Drop %W %X %Y"