Skip to content

Instantly share code, notes, and snippets.

@jlsajfj
Last active June 28, 2021 14:32
Show Gist options
  • Save jlsajfj/0bf429218830dc696c8ad99fcb2703bc to your computer and use it in GitHub Desktop.
Save jlsajfj/0bf429218830dc696c8ad99fcb2703bc to your computer and use it in GitHub Desktop.
Use a window as a screensaver on a second monitor
import win32gui, win32con, time
import tkinter as tk
import pywintypes
windowList = []
def winEnumHandler( hwnd, ctx ):
if win32gui.IsWindowVisible( hwnd ):
windowList.append((hwnd, win32gui.GetWindowText( hwnd )))
win32gui.EnumWindows( winEnumHandler, None )
window = tk.Tk()
wtitle = tk.Label(text="Select a window to use:")
wtitle.pack()
sWindow = None
sTitle = None
def selected( elem ):
global sWindow
global sTitle
sWindow = elem[0]
sTitle = elem[1]
window.destroy()
wbody = tk.Frame(window)
for windowElem in windowList:
if len(windowElem[1]) != 0:
thisName = tk.Button(wbody, text=windowElem[1],command=lambda val=windowElem: selected(val))
thisName.pack()
wbody.pack(side = tk.BOTTOM)
print("Selecting idle window")
window.mainloop()
print("{} selected".format(sTitle))
px, py = 0, 0# previous mouse location
timer = 0# idle timer
active = False# is idle window open
while True:
_,_, (x, y) = win32gui.GetCursorInfo()
dy, dx = abs(y-py), abs(x-px)
py, px = y, x
if x >= 0 or dy*dy + dx*dx < 100:
timer += 0.01
else:
if active:
print("No longer idling.")
win32gui.ShowWindow(sWindow, win32con.SW_SHOWMINNOACTIVE)
active = False
timer = 0
if timer > 10 and not active:
print("Now idling.")
cwin = win32gui.GetForegroundWindow()
active = True
win32gui.ShowWindow(sWindow, win32con.SW_MAXIMIZE)
try:
win32gui.SetForegroundWindow(cwin)
except pywintypes.error as e:
if e.args[2] == 'Access is denied.':# hacky solution around access denied
win32gui.ShowWindow(cwin, win32con.SW_MINIMIZE)
win32gui.ShowWindow(cwin, win32con.SW_RESTORE)
else:
raise e
time.sleep(0.01)
@jlsajfj
Copy link
Author

jlsajfj commented Jun 27, 2021

This was fun.

Basically just select a window to use, then when the mouse isn't moving considerably or it hasn't been on that monitor, the idle window will be maximized.

As soon as the mouse moves onto that monitor, it is minimized. Timeout is 10 seconds on line 53.

@jlsajfj
Copy link
Author

jlsajfj commented Jun 27, 2021

Might turn this into a repo... if I choose to keep working on it.

@jlsajfj
Copy link
Author

jlsajfj commented Jun 27, 2021

comments are basically just a post it board for myself.

there's a pretty big issues when maximizing, it loses your current active window. I can't find the exact window command to reactivate a window.

@jlsajfj
Copy link
Author

jlsajfj commented Jun 28, 2021

Added some print statements to tell if the system is in idle or not. Will throw access denied if run in non-admin due to setforeground requiring admin. Gonna add except statemtns soon

@jlsajfj
Copy link
Author

jlsajfj commented Jun 28, 2021

This should be a repo

@jlsajfj
Copy link
Author

jlsajfj commented Jun 28, 2021

I think im done though... added more prints, added workaround for access denied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment