Apr-14-2022, 08:00 PM
This works fine for me.
import tkinter as tk from ctypes import Structure, windll, c_uint, sizeof, byref class LASTINPUTINFO(Structure): _fields_ = [ ('cbSize', c_uint), ('dwTime', c_uint), ] def get_idle_duration(): lastInputInfo = LASTINPUTINFO() lastInputInfo.cbSize = sizeof(lastInputInfo) windll.user32.GetLastInputInfo(byref(lastInputInfo)) count = windll.kernel32.GetTickCount() return (count - lastInputInfo.dwTime) / 1000.0 class Window(tk.Tk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.counter = tk.IntVar(self, 0) self.timer = tk.DoubleVar(self, 0.0) tk.Label(self, textvariable=self.counter, width=20).pack(padx=10, pady=10) tk.Button(self, text="Iconify", command=self.iconify).pack(padx=10, pady=10) self.update() def update(self): self.counter.set(self.counter.get() + 1) if get_idle_duration() > 10.0: print("Time to wake up!") self.deiconify() self.after(1000, self.update) Window().mainloop()It also works if the Iconify button calls withdraw() instead of iconify().