I’ve read through related answers and it seems that the accepted way to do this is by binding callbacks to <Map>
and <Unmap>
events in the Toplevel widget. I’ve tried the following but to no effect:
from Tkinter import * tk = Tk() def visible(event): print 'visible' def invisible(event): print 'invisible' tk.bind('<Map>', visible) tk.bind('<Unmap>', invisible) tk.mainloop()
I’m running python 2.7 on Linux. Could this be related to window manager code in different operating systems?
Calling tk.iconify()
before tk.mainloop()
has no effect either. In fact, the only command that produces the correct behavior is tk.withdraw()
which is certainly not the same thing as minimizing the window. Additionally, if <Map>
and <Unmap>
events are triggered by calling pack()
, grid()
, or place()
, why is <Map>
triggered when the application window is minimized on Windows and/or Mac, as this and this answer suggest. And why would they be triggered when calling withdraw()
and deiconify()
on Linux?
Advertisement
Answer
Unmapping on Linux
The term Unmap
has a quite different meaning on Linux than it has on Windows. On Linux, Unmapping a window means making it (nearly) untraceable; It does not appear in the application’s icon, nor is it listed anymore in the output of wmctrl -l
. We can unmap / map a window by the commands:
xdotool windowunmap <window_id>
and:
xdotool windowmap <window_id>
To see if we can even possibly make tkinter detect the window’s state minimized, I added a thread to your basic window, printing the window’s state once per second, using:
root.state()
Minimized or not, the thread always printed:
normal
Workaround
Luckily, if you must be able to detect the window’s minimized state, on Linux we have alternative tools like xprop
and wmctrl
. Although as dirty as it gets, it is very well scriptable reliably inside your application.
As requested in a comment, below a simplified example to create your own version of the bindings with external tools.
How it works
- When the window appears (the application starts), We use
wmctrl -lp
to get the window’sid
by checking both name and pid (tkinter
windows have pid 0). - Once we have the
window id
, we can check if the string_NET_WM_STATE_HIDDEN
is in output ofxprop -id <window_id>
. If so, the window is minimized.
Then we can easily use tkinter
‘s after() method to include a periodic check. In the example below, the comments should speak for themselves.
What we need
We need both wmctrl and xprop to be installed. On Dedian based systems:
sudo apt-get install wmctrl xprop
The code example
import subprocess import time from Tkinter import * class TestWindow: def __init__(self, master): self.master = master self.wintitle = "Testwindow" self.checked = False self.state = None button = Button(self.master, text = "Press me") button.pack() self.master.after(0, self.get_state) self.master.title(self.wintitle) def get_window(self): """ get the window by title and pid (tkinter windows have pid 0) """ return [w.split() for w in subprocess.check_output( ["wmctrl", "-lp"] ).decode("utf-8").splitlines() if self.wintitle in w][-1][0] def get_state(self): """ get the window state by checking if _NET_WM_STATE_HIDDEN is in the output of xprop -id <window_id> """ try: """ checked = False is to prevent repeatedly fetching the window id (saving fuel in the loop). after window is determined, it passes further checks. """ self.match = self.get_window() if self.checked == False else self.match self.checked = True except IndexError: pass else: win_data = subprocess.check_output(["xprop", "-id", self.match]).decode("utf-8") if "_NET_WM_STATE_HIDDEN" in win_data: newstate = "minimized" else: newstate = "normal" # only take action if state changes if newstate != self.state: print newstate self.state = newstate # check once per half a second self.master.after(500, self.get_state) def main(): root = Tk() app = TestWindow(root) root.mainloop() if __name__ == '__main__': main()