event generate

event generate window event ?option value option value ...?

See the event command documentation.

Generates a window event and arranges for it to be processed just as if it had come from the window system. Window gives the path name of the window for which the event will be generated; it may also be an identifier (such as returned by winfo id) as long as it is for a window in the current application. Event provides a basic description of the event, such as <Shift-Button-2> or <<Paste>>. If Window is empty the whole screen is meant, and coordinates are relative to the screen. Event may have any of the forms allowed for the sequence argument of the bind command except that it must consist of a single event pattern, not a sequence. Option-value pairs may be used to specify additional attributes of the event, such as the x and y mouse position; see EVENT FIELDS . If the -when option is not specified, the event is processed immediately: all of the handlers for the event will complete before the event generate command returns. If the -when option is specified then it determines when the event is processed. Certain events, such as key events, require that the window has focus to receive the event properly.

How to "warp" the mouse pointer to some other position:

event generate $window <Motion> -x $x -y $y -warp 1

How to simulate a mouse click?

If you wish to generate the mouse click event on a button, but have no need to animate the button (you don't want to mimic the appearance of a real mouse click), then simply use the "invoke" command of the button widget:

.butt invoke

If you do want visual feedback for a simulated button click, you may generate a button-press event followed by a button-release event:

event generate .butt <1>
after 100 {event generate .butt <ButtonRelease-1>}

The delay (100 milliseconds here) is necessary to allow the button widget to be shown depressed before it is restored. Without the delay the result would be just like .butt invoke.

JE Another way, which works for buttons, checkbuttons, and radiobuttons, is the (internal, officially unsupported, but unlikely to go away) routine *tk::ButtonInvoke*.

For ttk::* widgets, you can use

event generate $b <<Invoke>>

The <<Invoke>> event is recognized by all ttk::* widgets that have mnemonic support (i.e., anything with -text and -underline options). See also the keynav package [L1 ], which adds <<Invoke>> bindings for all the core widgets as well.

Other button clicks carry more information - think of drawing a picture on a canvas widget - and this information can also be generated as part of the mouse click event. This example moves the mouse pointer to a specific location on a canvas and generates a mouse click there; the simulated click changes a rectangle from red to green:

package require Tk
pack [canvas .c]
set id [.c create rect 10 10 50 50 -fill red]
.c bind $id <ButtonRelease-1> [list %W itemconfigure $id -fill green]
raise .
# After 3 seconds, simulate a mouse click on the rectangle:
after 3000 {
    event generate .c <Motion> -x 30 -y 30 -warp 1
    event generate .c <Enter>
    event generate .c <ButtonPress-1> -x 30 -y 30
    after 50 { event generate .c <ButtonRelease-1> }

A variation on that is to generate a mouse click wherever the mouse pointer happens to be on the canvas. Say we place a dot on the canvas for every mouse click (real or generated) and automatically generate a mouse click every second, in addition to any real mouse clicks:

package require Tk
proc makedot {W x y} {
  $W create oval [expr {$x-5}] [expr {$y-5}] [expr {$x+5}] [expr {$y+5}] -fill red
pack [canvas .c]
bind .c <ButtonRelease-1> {makedot %W %x %y} 
# Generate a click every second, 
proc dotty { } {
    set X [expr [winfo pointerx .] - [winfo rootx .]]
    set Y [expr [winfo pointery .] - [winfo rooty .]]   
    event generate .c <ButtonPress-1> -x $X -y $Y
    event generate .c <ButtonRelease-1> -x $X -y $Y
    after 1000 dotty
after 1000 dotty

You can encapsulate both a button press and a button release in a <<Click>> event like this

bind all <<Click>> {
    event generate %W <1>
    after 100 {event generate %W <ButtonRelease-1>}

and these "Click" events can then be generated like

event generate .b <<Click>>

See also: