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's id
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 of xprop -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()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…