Tk Source Code

Artifact [c3f1ab37]
Login

Artifact c3f1ab37284bc01ca4ed75ea99618c501de585442ce65ba481dc3108b2ca77da:

Attachment "exercise-2.tcl" to ticket [0008d6a6] added by erikleunissen 2020-09-30 21:01:18.
#! /usr/bin/tclsh

package require Tk
wm withdraw .

#
# Wait for a specified duration while allowing events to be processed
#
proc twait {duration} {
	set ::_twait 1; after $duration {unset ::_twait}
	vwait ::_twait
}

toplevel .a -bg yellow
pack [frame .a.f -bg green -width 50 -height 50]
wm deiconify .a
pack propagate .a 0
twait 200

proc getPointerWindow {} {
	return [winfo containing [winfo pointerx .] [winfo pointery .]]
}

#
# Bind on <Destroy> event
#
proc handleWindowDestruction {W} {
	puts "\n*** destroy event handler"
	puts "Window $W exists: [winfo exists $W]"
	puts "Window containing mouse pointer: \"[getPointerWindow]\""
}
bind .a.f <Destroy> {handleWindowDestruction %W}

#
# Leave trace on [destroy], in which [winfo containing] returns the correct window
#
proc handleDestroyTrace {args} {
	set tracedCommand [lindex $args 0]
	set W [lindex $tracedCommand 1]
	puts "\n*** trace handler on $tracedCommand"
	puts "Window $W exists: [winfo exists $W]"
	puts "Window containing mouse pointer: \"[getPointerWindow]\""
}
trace add execution destroy leave handleDestroyTrace


# Position the mouse pointer over frame .a.f
event generate .a.f <Motion> -warp 1 -x 25 -y 25 -when now
twait 1000; # excessively long, in order to satisfy the human eye.

destroy .a.f