1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| import time, threading, random
import win32gui, win32con, win32api
import ctypes
import tkinter as tk
from PIL import ImageTk, Image, ImageOps
####PARAMETRES####
IdleTime = 1000 #msec
####VARIABLES####
wallpaper = None
wallpaper_path = None
wallpaperized = False
idle = False
win_title = str(random.getrandbits(128)) #Nom de fenêtre Tkinter random
def getIdleTime():
return (win32api.GetTickCount() - win32api.GetLastInputInfo())
# Afficher le bureau quand on clic gauche sur le wallpaper
def on_click(event):
global walpaperized, idle
root.withdraw()
walpaperized = False
idle = False
#Met à jour le canvas (si changement de wallpaper)
def getWallpaper():
global wallpaper, wallpaper_path
ubuf = ctypes.create_unicode_buffer(512)
ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_GETDESKWALLPAPER,len(ubuf),ubuf,0)
path = ubuf.value
if wallpaper_path != path:
wallpaper_path = path
wallpaper = Image.open(path)
wallpaper = ImageOps.fit(wallpaper, (WIDTH,HEIGHT)) #Redimensionne l'image à la taille de l'écran
wallpaper = ImageTk.PhotoImage(wallpaper) #Transforme l'image pour Tkinter
canvas.itemconfig(container, image=wallpaper) #Met à jour le canvas
#Affiche la fenêtre tkinter
def displayWallpaper():
root.deiconify()
#Retourne False si une fenêtre est ouverte (sauf root et qq programmes windows), une exception est alors soulevée par EnumWindows
def winEnumHandler( hwnd, ctx ):
if win32gui.IsWindowVisible( hwnd ) and not win32gui.IsIconic(hwnd):
title = win32gui.GetWindowText( hwnd )
for word in ('','Expérience dentrée Windows','Program Manager','Paramètres','Hôte contextuel',\
'Centre de notifications','Calculatrice','Alarmes et horloge',win_title):
if title == word:
return True
return False
#Fenêtre tkinter
def mainroot():
global WIDTH, HEIGHT, root, container, canvas
root = tk.Tk()
root.title(win_title)
WIDTH, HEIGHT = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry(f'{WIDTH+4}x{HEIGHT+4}+-2+-2')
#root.attributes('-fullscreen',True )
root.overrideredirect(1) #Mettre la fenêtre par dessus tout
#root.attributes('-topmost',True )
canvas = tk.Canvas(root, width = WIDTH, height = HEIGHT)
canvas.pack()
container = canvas.create_image(2,2, anchor=tk.NW) #Garder le canvas en mémoire
root.withdraw() #Cacher la fenêtre
root.bind("<ButtonRelease-1>", on_click) #"Binder" la fenêtre entière pour la cacher
root.mainloop()
threading.Thread(target=mainroot).start()
time.sleep(1)
while True:
time.sleep(0.1)
getWallpaper()
try:
win32gui.EnumWindows( winEnumHandler, None )
except: #Une fenêtre est ouverte (exceptée celles autorisées)
wallpaperized = False
idle = False
else: #Si aucune exception soulevée alors aucune fenêtre n'est ouverte, on est donc sur le bureau grosso-modo
title = win32gui.GetWindowText(win32gui.GetForegroundWindow())
if not title: #On est vraiment sur le bureau
if not wallpaperized: #On n'a pas déjà affiché le wallpaper (sécurité inutile ?)
wallpaperized = True
threading.Thread(target=displayWallpaper).start() #On affiche le wallpaper
if not idle:
if getIdleTime()>IdleTime :
if title not in ('Calculatrice','Alarmes et horloge'): #Nécessaire pour ne pas afficher le wallpaper par dessus ces deux utilitaires windows
idle = True
wallpaperized = True
threading.Thread(target=displayWallpaper).start() |
Partager