app.py
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 |
import tkinter as tk from tkinter import filedialog, messagebox import ctypes import os import shutil from PIL import Image # Constants SPI_SETDESKWALLPAPER = 20 APPDATA_PATH = os.getenv('APPDATA') WALLPAPER_FOLDER = os.path.join(APPDATA_PATH, 'WallpaperChanger') os.makedirs(WALLPAPER_FOLDER, exist_ok=True) DEFAULT_BMP = os.path.join(WALLPAPER_FOLDER, 'original_wallpaper_backup.bmp') TEMP_BMP = os.path.join(WALLPAPER_FOLDER, 'temp_wallpaper.bmp') # Get current wallpaper (Windows only) def get_current_wallpaper(): buf = ctypes.create_unicode_buffer(300) ctypes.windll.user32.SystemParametersInfoW(0x0073, 300, buf, 0) return buf.value # Set wallpaper via ctypes (must be BMP) def set_wallpaper(image_path): if not os.path.exists(image_path): raise FileNotFoundError(f"Wallpaper path not found: {image_path}") ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, 3) # Convert image to BMP and return path def convert_to_bmp(source_path, destination_path): image = Image.open(source_path) image.save(destination_path, 'BMP') return destination_path # Button: Browse image and set as wallpaper def browse_image(): file_path = filedialog.askopenfilename( filetypes=[("Image Files", "*.png *.jpg *.jpeg *.bmp *.gif")] ) if file_path: try: # Backup current wallpaper if not already if not os.path.exists(DEFAULT_BMP): current = get_current_wallpaper() if os.path.exists(current): shutil.copy(current, DEFAULT_BMP) # Convert to BMP bmp_path = convert_to_bmp(file_path, TEMP_BMP) # Set as wallpaper set_wallpaper(bmp_path) messagebox.showinfo("Success", "Wallpaper changed successfully!") except Exception as e: messagebox.showerror("Error", str(e)) # Button: Restore original wallpaper def restore_default_wallpaper(): if os.path.exists(DEFAULT_BMP): try: set_wallpaper(DEFAULT_BMP) messagebox.showinfo("Reverted", "Wallpaper restored to original.") except Exception as e: messagebox.showerror("Error", str(e)) else: messagebox.showwarning("No Backup", "Original wallpaper backup not found.") # GUI setup root = tk.Tk() root.title("Wallpaper Changer") root.geometry("360x200") root.resizable(False, False) tk.Label(root, text="Desktop Wallpaper Manager", font=("Arial", 14, "bold")).pack(pady=10) tk.Button(root, text="Select Image and Set Wallpaper", command=browse_image, width=35).pack(pady=10) tk.Button(root, text="Restore Original Wallpaper", command=restore_default_wallpaper, width=35).pack(pady=5) tk.Button(root, text="Exit", command=root.quit, width=35).pack(pady=5) root.mainloop() |