|
| 1 | +from tkinter import Tk, Label, Button # from Tkinter import Tk for Python 2.x |
| 2 | +from tkinter.filedialog import askopenfilename |
| 3 | +import subprocess |
| 4 | +from sys import platform |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# Permitted Extensions |
| 8 | +ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png'] |
| 9 | + |
| 10 | +# Terminal command to change the background image (this may be different for different Linux distributions) |
| 11 | +LINUX_TERMINAL_COMMAND = ["gsettings", "set", "org.gnome.desktop.background", "picture-uri"] |
| 12 | +filename = None |
| 13 | + |
| 14 | +def get_file(): |
| 15 | + global filename |
| 16 | + filename = askopenfilename(title="Choose an image", filetypes=[("Image files", ("*.jpg", "*.jpeg", "*.png"))]) |
| 17 | + |
| 18 | + # Check for extension and if it is not allowed set the file to None |
| 19 | + if not filename.split(".")[-1] in ALLOWED_EXTENSIONS: |
| 20 | + print("Please select a JPEG/PNG file") |
| 21 | + filename = None |
| 22 | + |
| 23 | + print("Selected Image: ", filename) |
| 24 | + |
| 25 | +def set_wallpaper(): |
| 26 | + # Return if an image file is not selected |
| 27 | + if filename is None: |
| 28 | + return |
| 29 | + # Identify the user's platform (Windows, Linux or Mac OS) |
| 30 | + user_os = platform.lower() |
| 31 | + |
| 32 | + if user_os in ["linux", "linux2"]: |
| 33 | + # Convert the image path into a file uri |
| 34 | + file_uri = Path(filename).as_uri() |
| 35 | + |
| 36 | + # Execute the terminal command to change the wallpaper using the image's file uri |
| 37 | + process = subprocess.Popen(LINUX_TERMINAL_COMMAND + [file_uri], stdout=subprocess.PIPE) |
| 38 | + stdout = process.communicate() |
| 39 | + process.terminate() |
| 40 | + |
| 41 | + # Check for any errors and print them |
| 42 | + if stdout[1]: |
| 43 | + print(stdout) |
| 44 | + else: |
| 45 | + print("Wallpaper Set Successfully!") |
| 46 | + else: |
| 47 | + print("Your operating system is currently not supported") |
| 48 | + |
| 49 | + |
| 50 | +# Tkinter GUI |
| 51 | +window = Tk() |
| 52 | +window.title("Python World") |
| 53 | +title = Label(text="Linux Wallpaper Changer", height=2, width=40).pack() |
| 54 | + |
| 55 | +# Buttons to get file path and set the wallpaper |
| 56 | +file_chooser = Button(text="Choose an image (JPEG or PNG)", height=1, width=30, command=get_file).pack(pady=5) |
| 57 | +submit_button = Button(text="Set as wallpaper", height=2, width=30, command=set_wallpaper).pack(pady=5) |
| 58 | +window.mainloop() |
0 commit comments