Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit f24fdc6

Browse files
authored
Merge pull request #449 from ritesh-dt/linux-wallpaper-change
feat: Change Wallpaper using Python (for Linux)
2 parents e70a8ab + 3d9e02a commit f24fdc6

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Linux Wallpaper Changer
2+
<!--Remove the below lines and add yours -->
3+
Python script which change the desktop wallpaper of Linux-based operating system.
4+
5+
### Prerequisites
6+
<!--Remove the below lines and add yours -->
7+
The script does not need any external modules. It uses in-built python libraries like pathlib, sys, Tkinter, etc.
8+
9+
### How to run the script
10+
<!--Remove the below lines and add yours -->
11+
Make sure you have Python 3.x installed on your machine then,
12+
1. Go to **Scripts/Miscellaneous/Linux_Wallpaper_Changer** in *Command Prompt* or *Terminal*
13+
2. Run the following command.
14+
`python3 linux_wallpaper_changer.py`
15+
or `python linux_wallpaper_changer.py`
16+
17+
18+
### Screenshot/GIF showing the sample use of the script
19+
<!--Remove the below lines and add yours -->
20+
##### Before
21+
![Screenshot-Before](https://i.postimg.cc/cCxVQ5gz/wallpaper-before.png)
22+
##### After
23+
![Screenshot-After](https://i.postimg.cc/HnbVynMz/wallpaper-after.png)
24+
25+
## *Author Name*
26+
<!--Remove the below lines and add yours -->
27+
[Ritesh T](https://github.com/ritesh-dt)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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()
735 KB
Loading
194 KB
Loading

0 commit comments

Comments
 (0)