Tkinter - Checking If Application Has Focus
Solution 1:
I'm trying to subclass Tk where it pauses audio if and only if the entire application loses focus (i.e. the Tk instance loses focus and focus wasn't passed to a Toplevel or messagebox widget).
I think you get away with this:
import tkinter as tk
root = tk.Tk()
b = tk.Button(root, text='top', command= lambda: tk.Toplevel(root))
b.pack()
defcheck ():
condition = root.focus_get()
root.after(500, check)
if condition == None:
print('pause music')
pass
check()
root.mainloop()
Because root.focus_get() just returns None if all windows are minimized or loses focus. Most other methods I've tried was returning None as soon as the root was minimized.
This method also fails by a builtin messagebox, because these aren't related to the root window. Thats why you would need to build your own like:
def popupmsg(msg):
popup = tk.Toplevel()
popup.wm_title("Warning!")
label = tk.Label(popup, text=msg)
label.pack(side="top", fill=BOTH, expand=True)
B1 = tk.Button(popup, text="okay", command = popup.destroy)
B1.pack()
So the full exampel would be like this:
import tkinter as tk
from tkinter import messagebox
def popupmsg(msg):
popup = tk.Toplevel(root)
popup.wm_title("Warning!")
label = tk.Label(popup, text=msg)
label.pack(side="top", fill='both', expand=True)
B1 = tk.Button(popup, text="okay", command = popup.destroy)
B1.pack()
root = tk.Tk()
b = tk.Button(root, text='top', command= lambda: tk.Toplevel(root))
b.pack()
b1 = tk.Button(root, text='msg', command= lambda: messagebox.showinfo('title', 'msg'))
b1.pack()
b2 = tk.Button(root, text='popup', command= lambda: popupmsg('I dare you!'))
b2.pack()
def check ():
condition = root.focus_get()
root.after(500, check)
if condition == None:
print('pause music')
pass
check()
root.mainloop()
Another way
I found this code here: Determining what tkinter window is currently on top
and implemented in the code. It has the benefit that it works with messagebox, but only when another tk window that is related to your root is aktive. This code isn't about focus, it just tells you which window is currently on top.
So the conditions is all of the windows needs to be iconify.
import tkinter as tk
from tkinter import messagebox
def popupmsg(msg):
popup = tk.Toplevel(root)
popup.wm_title("Warning!")
label = tk.Label(popup, text=msg)
label.pack(side="top", fill='both', expand=True)
B1 = tk.Button(popup, text="okay", command = popup.destroy)
B1.pack()
root = tk.Tk()
b = tk.Button(root, text='top', command= lambda: tk.Toplevel(root))
b.pack()
b1 = tk.Button(root, text='msg', command= lambda: [root.iconify(), messagebox.showinfo('title', 'msg')])
b1.pack()
b2 = tk.Button(root, text='popup', command= lambda: popupmsg('I dare you!'))
b2.pack()
def check ():
condition1 = root.tk.eval('wm stackorder '+str(root))
root.after(500, check)
if condition1 == "":
print('pause music')
pass
else:
print('return music')
check()
root.mainloop()
Post a Comment for "Tkinter - Checking If Application Has Focus"