0

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() 
user3722736
  • 191
  • 1
  • 2
  • 12
  • Are you asking about how to detect if they are inactive for the entire computer, or just inactive in using the tkinter app? For the former, it's probably going to require a platform-specific solution since tkinter has no way of knowing what you're doing in other apps. – Bryan Oakley Oct 18 '18 at 15:40
  • Yes, for the entire computer. I understand from here: https://stackoverflow.com/questions/217157/how-can-i-determine-the-display-idle-time-from-python-in-windows-linux-and-mac that it should be possible. – user3722736 Oct 18 '18 at 21:10

0 Answers0