tk_popup
Guide: Menus
Shows a menu as a popup at a point of the screen.
tk_popup MENU X Y ?ENTRY?
Description
Posts the menu widget MENU with its top left corner at X Y, in the coordinates of the screen, as the menu of a right click. The script of the -postcommand of the menu runs first. A click on an entry invokes it and closes the menu, and a click outside the menu closes it. The command gives at once, and the menu stays open.
A binding of <Button-3> usually calls it with the coordinates of the pointer, %X and %Y. On a touch screen a long press is a <Button-3> event, so the same binding opens the menu.
With ENTRY, ztk passes the entry to the post command of the menu.
On Windows and macOS, the menu is the popup menu of the platform. The other backends draw the menu with the widgets of ztk.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| MENU | window | required | The path of a menu widget. |
| X | integer | required | The left edge of the menu, in pixels from the left of the screen. |
| Y | integer | required | The top edge of the menu, in pixels from the top of the screen. |
| ENTRY | index | none | An index of an entry of the menu, for the place of the menu in Tk. |
Returns
An empty string.
Examples
% menu .m -tearoff 0 -postcommand {set ::opened 1}
> .m add command -label Copy
> .m add command -label Paste
> tk_popup .m 100 80
> .m unpost
> set ::opened
1
# The usual binding of a menu of a right click.
% frame .area -width 200 -height 100
> bind .area <Button-3> {tk_popup .m %X %Y}
> bind .area <Button-3>
tk_popup .m %X %Y
Errors
% tk_popup
wrong # args: should be "tk_popup menu x y ?entry?"
-errorcode TCL WRONGARGS
% tk_popup .nope 10 10
invalid command name ".nope"
-errorcode TCL LOOKUP COMMAND .nope
% button .b
> tk_popup .b 10 10
bad option "post": must be cget, configure, flash, or invoke
-errorcode TCL LOOKUP INDEX option post
% menu .m2
> tk_popup .m2 10 y
expected integer but got "y"
-errorcode TCL VALUE NUMBER
Differences from Tcl
- With ENTRY, the entry does not become active:
MENU index activegivesnone. Tk 8.6.16 places the menu so that ENTRY is at X Y, and makes ENTRY active. - A drawn menu after
tk_popupgives 0 forwinfo ismapped. In Tk 8.6.16 the posted menu gives 1. - An X or a Y that is not an integer gives the
-errorcodeTCL VALUE NUMBER. Tk 8.6.16 givesTCL VALUE INTEGER.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1