Version 17 of grab

Updated 2006-06-12 13:45:37

grab ?-global? window

grab current ?window?

grab release window

grab set ?-global? window

grab status window

Programmers should be cautious when issuing grabs. This basically takes control from the user, leaving them at the mercy of a program that might not be operating as expected...

Not only that, but if the user needs for instance to copy and pass data from another window, or needs to handle some emergency, and the app has issued a grab, the app is not going to be looked upon kindly.

Best don't do that. Joe English wrote in the comp.lang.tcl newsgroup:

My two rules for [grab -global]:

(1) don't use it unless I'm absolutely sure I know what I'm doing, and

(2) if I think I need it this is a sure sign that I don't know what I'm doing :-)

Bryan Oakley's advice for working with global grabs:

While in development, always put in a failsafe. For example, I'll typically add code such as "after 60000 exit" to quit the application in one minute. That way, if my code has a bug and the grab doesn't get released, my system is only frozen for a minute. Otherwise, depending on circumstances, the system might require a reboot.

Later, Bryan writes in comp.lang.tcl:

I use a global grab in my combobox code with great effect. I've not once heard of a complaint related to the grab (though maybe those poor souls are still locked out of their machine and can't send me a message..).

If memory serves, the main reason was to make it so the combobox dropdown goes away with any mouse click anywhere outside the combobox. A global grab was just the ticket.

But yes, global grabs should be avoided if at all possible. Even then, they should be avoided unless absolutely, positively unavoidable. And yet, even then you need to think twice before doing it :-)

The few times I've played with global grabs I've always (except for the first time...) set a timer that releases the grab after a minute or so, to make sure I don't hose myself. The first time, I didn't do this and the experience wasn't very pleasant.

In an entirely different direction, Geoff Battye raises subtle points about grab semantics in a newsgroup thread, the rough conclusion of which is that there ought to be a default mechanism to nest or stack them: "Donald Arseneau pointed out the Tk internal routines ::tk::SetFocusGrab and ::tk::RestoreFocusGrab to me ...

I went with a 'grab stack' in the end (not worrying about focus since it behaves as desired automatically for me).

One further issue is that grabs in included packages can also interfere. For this reason it may be a good idea to redefine the grab command to automatically nest grabs (renaming grab to do the actual grabbing within the new command). In fact, off the top of my head, I can't think why automatically nesting grabs shouldn't be the default behaviour of grab anyway. However, I don't encounter this issue at the moment."

A completely different sense of "grab" is the one of the Macintosh application of that name, also regarded as a "Robot" function by Javaists (but see also "Capture a window into an image"): capture of the coloring of a portion of the screen. zoro2 suggests for this:

    proc snapsomearea {area} {
        set p [toplevel .tmp[incr ::someCounter]]
        pack [label $p.l -fill both -expand 1]
        wm overrideredirect $p 1
        wm geometry $p $area
        lower $p
        set snap [blt::winop snap $p.l]
        destroy $p.l
        return $snap


    set fh [open "|xwd -root -silent | xwdtoppm | ppmtopng" r]
    fconfigure $fh -translation binary
    set fc [read $fh]
    close $fh
    image create photo -data $fc

while dkf takes the approach of

    exec xwd -root -silent | xwdtoppm | ppmtopng >screendump.png 

Category Command in the Tk package - Tk syntax help - Arts and Crafts of Tcl-Tk Programming