1
votes

I would like to create a binding for the toplevel window state to execute commands if the window is in fullscreen mode (zoomed state) or not. I've been looking for how to do that, and I've come across this post: How to catch the maximize signal in Tk?

I'm essentially trying to do the same thing now. That question was posted 3 years ago, have there been any updates to allow Tk to deal with the maximize button? If not, could someone explain what the following code does?

bind $toplvl <Configure> {
    if {"%W" eq [winfo toplevel "%W"]} {
        ActOnResize %W %w %h [wm attributes %W -zoomed]
    }
}
1

1 Answers

1
votes

OK, let's look at the three “interesting” lines, one by one.

bind $toplvl <Configure> {

This attaches a binding to the <Configure> event to the binding tag in $toplvl, which I presume is the name of a toplevel widget. The <Configure> event is delivered when the size or position of a widget is changed (plus a few technical things that Tk doesn't really use).

if {"%W" eq [winfo toplevel "%W"]} {

First off, %W is replaced with the name of the widget that the event was dispatched to. This line makes what follows conditional on the event being delivered to an actual toplevel widget, which is useful because toplevels also receive events sent to all their children (great for binding hotkeys, not so great for other events).

ActOnResize %W %w %h [wm attributes %W -zoomed]

This calls the command ActOnResize, passing in four arguments: the (toplevel) widget name, the new width, the new height, and whether the toplevel is zoomed or not (which has to be retrieved with that call to wm attributes).

High Level Summary

Call ActOnResize whenever the toplevel changes size or is moved relative to its parent window (which is logically the desktop root), passing in its new size and a boolean that indicates if it's maximized.