I'm trying to have a thread/process that runs alongside my Tkinter event loop and constantly checks to see if a certain amount of time has elapsed since the user was last active so that a screensaver can be displayed.
Unfortunately I keep encountering errors. I tried to use a thread with a while True loop but then the event loop would not work.
from tkinter import *
import screensaver
import time
import threading
def screen_save():
while True:
if (time.time() - last_time_active > 30):
screensaver()
last_time_active = time.time()
def refresh_time():
global last_time_active
last_time_active = time.time()
screen = Tk()
button = Button(screen, text="activity", command=refresh_time)
thread = threading.Thread(target=screen_save)
thread.start()
mainloop()
I also tried an approach using .after and recursion but that instantly exceeded the maximum recursion depth despite being instruction to wait 30 seconds each .after call.
from tkinter import *
import screensaver
import time
def screen_save():
if (time.time() - last_time_active > 30):
screensaver()
else:
screen.after(5000, screen_save)
last_time_active = time.time()
def refresh_time():
global last_time_active
last_time_active = time.time()
screen = Tk()
button = Button(screen, text="activity", command=refresh_time)
screen.root(5000, screen_save)
mainloop()
Any advice would be greatly appreciated!
Also, in the minimal version of the code I included, screensaver is a working function I made that displays a working Tkinter screensaver. I just retyped what I essentially did.