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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| import time, threading, random, os
import win32gui, win32con, win32api
import ctypes
import tkinter as tk
from PIL import ImageTk, Image
import locale
locale.setlocale(locale.LC_TIME,'')
####PARAMETRES####
IdleTime = 1000 #msec
####VARIABLES####
wallpaper = None
wallpaper_path = None
wallpaperized = False
idle = False
win_title = 'WALLPAPER.py-'+str(random.getrandbits(128)) #Nom de fenêtre Tkinter random
chemin = os.path.expanduser('~\Wallpaper')
def getIdleTime():
return (win32api.GetTickCount() - win32api.GetLastInputInfo())
# Afficher le bureau quand on clic gauche sur le wallpaper
def on_click(event):
global idle
root.withdraw()
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)
w, h = wallpaper.size
ratio_w = w/WIDTH
ratio_h = h/HEIGHT
if ratio_w > ratio_h:
W, H = (int(w/ratio_w), int(h/ratio_w))
wallpaper = wallpaper.resize((W, H))
black = Image.new('RGB', (WIDTH, HEIGHT), color = 'black')
black.paste(wallpaper, (int((WIDTH-W)/2), int((HEIGHT-H)/2)))
wallpaper = black.copy()
elif ratio_w <= ratio_h:
W, H = (int(w/ratio_h), int(h/ratio_h))
wallpaper = wallpaper.resize((W, H))
black = Image.new('RGB', (WIDTH, HEIGHT), color = 'black')
black.paste(wallpaper, (int((WIDTH-W)/2), int((HEIGHT-H)/2)))
wallpaper = black.copy()
#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()
#TITLE = []
#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, liste ):
if win32gui.IsWindowVisible( hwnd ) and not win32gui.IsIconic(hwnd):
title = win32gui.GetWindowText( hwnd )
liste.append((hwnd, title))
for word in ('','Expérience dentrée Windows','Program Manager','Paramètres','Hôte contextuel',\
'Centre de notifications','Calculatrice','Alarmes et horloge','Drag','Pense-bêtes', 'Gestionnaire des tâches', win_title):
if title == word:
break
#return True
else:
return False
#Fenêtre tkinter
def mainroot():
global WIDTH, HEIGHT, root, container, canvas, canvas2, hour_b, hour_f, day_l, day_r, day_f
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)
container = canvas.create_image(2,2, anchor=tk.NW) #Garder le canvas en mémoire
date = time.strftime('%A, %d %B, %Y %H:%M:%S').capitalize()
date = date.split(' ')
hour = date[-1]
day = ' '.join((date[0:-1]))
hour_b = canvas.create_text(int(WIDTH/2),int(HEIGHT/2)-50, font=('French Script MT', 200, 'bold'), fill='black', text = hour)
hour_f = canvas.create_text(int(WIDTH/2),int(HEIGHT/2)-50, font=('French Script MT', 200), fill='white', text = hour)
day_l = canvas.create_text(int(WIDTH/2)-1,int(HEIGHT/2)+90, font=('French Script MT', 50), fill='black', text = day)
day_r = canvas.create_text(int(WIDTH/2)+1,int(HEIGHT/2)+90, font=('French Script MT', 50), fill='black', text = day)
day_f = canvas.create_text(int(WIDTH/2),int(HEIGHT/2)+90, font=('French Script MT', 50), fill='white', text = day)
canvas.pack()
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, daemon=True).start()
time.sleep(1)
while True:
time.sleep(0.1)
getWallpaper()
try:
liste = []
win32gui.EnumWindows( winEnumHandler, liste )
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, daemon=True).start() #On affiche le wallpaper
if not idle:
if getIdleTime()>IdleTime :
idle = True
wallpaperized = True
threading.Thread(target=displayWallpaper, daemon=True).start()
#for fen in liste:
# hwnd, titre = fen
# if titre in ('Calculatrice','Alarmes et horloge','Pense-bêtes'): #Afficher ces trois utilitaires windows par dessus le wallpaper
# try:
# win32gui.SetForegroundWindow(hwnd)
# except:
# print(titre,'failed to setforeground')
date = time.strftime('%A, %d %B, %Y %H:%M:%S').capitalize()
date = date.split(' ')
hour = date[-1]
day = ' '.join((date[0:-1]))
canvas.itemconfig(hour_b, text=hour)
canvas.itemconfig(hour_f, text=hour)
canvas.itemconfig(day_l, text=day)
canvas.itemconfig(day_r, text=day)
canvas.itemconfig(day_f, text=day) |
Partager