Binding callbacks to minimize and maximize events in Toplevel windows
<p>I've read through related answers and it seems that the accepted way to do this is by binding callbacks to <code>&lt;Map&gt;</code> and <code>&lt;Unmap&gt;</code> events in the Toplevel widget. I've tried the following but to no effect:</p>

<pre><code>from Tkinter import *

tk = Tk()

def visible(event):
    print 'visible'

def invisible(event):
    print 'invisible'

tk.bind('&lt;Map&gt;', visible)
tk.bind('&lt;Unmap&gt;', invisible)

tk.mainloop()
</code></pre>

<p>I'm running python 2.7 on Linux. Could this be related to window manager code in different operating systems?</p>

<p>Calling <code>tk.iconify()</code> before <code>tk.mainloop()</code> has no effect either. In fact, the only command that produces the correct behavior is <code>tk.withdraw()</code> which is certainly not the same thing as minimizing the window. Additionally, if <code>&lt;Map&gt;</code> and <code>&lt;Unmap&gt;</code> events are triggered by calling <code>pack()</code>, <code>grid()</code>, or <code>place()</code>, why is <code>&lt;Map&gt;</code> triggered when the application window is minimized on Windows and/or Mac, as <a href="http://stackoverflow.com/a/3840216/2592879">this</a> and <a href="http://stackoverflow.com/a/26160588/2592879">this</a> answer suggest. And why would they be triggered when calling <code>withdraw()</code> and <code>deiconify()</code> on Linux?</p>

<p>For me, on Win10, your code works perfectly, with the caveat that the middle frame button produces 'visible' whether it means 'maximize' or 'restore'.  So maximize followed by restore results in 2 new 'visibles' becoming visible.</p>

<p>I did not particularly expect this because <a href="http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-types.html" rel="nofollow">this reference</a> says Map is produced when </p>

<blockquote>
  <p>A widget is being mapped, that is, made visible in the application. This will happen, for example, when you call the widget's .grid() method. </p>
</blockquote>

<p>Toplevels are not managed by geometry.  The more authoritative <a href="http://www.tcl.tk/man/tcl8.6/TkCmd/bind.htm#M13" rel="nofollow">tk doc</a> says </p>

<blockquote>
  <p>Map, Unmap</p>
  
  <p>The Map and Unmap events are generated whenever the mapping state of a window changes.</p>
  
  <p>Windows are created in the unmapped state. Top-level windows become mapped when they transition to the normal state, and are unmapped in the withdrawn and iconic states.</p>
</blockquote>

<p>Try adding <code>tk.iconify()</code> before the mainloop call.  This should be doing the same as the minimize button.  If it does not result in 'invisible', then there appears to be a tcl/tk bug on Linux.</p>

python linux tkinter