I would like to build a program in Python resembling screensaver behaviour, but I'm not sure how to implement the part where I check how long the user was inactive and open the Tkinter window only when it reaches a certain value. I want to launch this python script on user login, to run in the background, and only open the Tkinter window when user becomes inactive for, say a minute.
I have the main Tkinter window working but I'm not sure where to put the loop(?) to keep checking the time since last user activity, or perhaps I should use the existing mainloop at the end somehow?
This is my code to get idle time value:
def getIdleTime():
last_active = win32api.GetLastInputInfo()
now = win32api.GetTickCount()
elapsed_seconds = (now - last_active)/1000
Somewhere in my code I need to keep checking the value of elapsed seconds, and when it reaches 60, open the Tk window.
My Tkinter program looks a bit like this:
import tkinter as tk
import random
import pandas as pd
import os
import win32api
#some functions
# --- main ---
root = tk.Tk()
root.attributes('-fullscreen', True)
root.bind("<Return>",change_text_on_click)
root.bind("<Escape>",screen_exit)
c = tk.Canvas(root, bg='black')
c.pack(expand=1, fill=tk.BOTH)
w = c.create_text(
400, 500,
anchor=tk.CENTER,
font=("Calibri",70),
fill='white',
width=root.winfo_screenwidth()*0.8,
text='' # empty string populated by change_text function
)
# set first text
change_text()
root.mainloop()