Last active
June 28, 2021 14:32
-
-
Save jlsajfj/0bf429218830dc696c8ad99fcb2703bc to your computer and use it in GitHub Desktop.
Use a window as a screensaver on a second monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This should be a repo
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
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