Build an Application for Changing PC's Wallpaper using Python - GeeksforGeeks

04 Dec, 2021
Prerequisite: Python GUI – tkinter
In this article, we are going to write a script for background changing application using the py-wallpaper module in Python. The py-wallpaper module is used to change the background wallpaper.  Before starting we need to install py-wallpaper.
Installation:
To install this type the below command in the terminal.
pip install py-wallpaper
Import modules.
Python3
# import modulesfrom wallpaper import set_wallpaper, get_wallpaper
Now you can get your current background image location with get_wallpaper attributes and you can change with set_wallpaper.
Python3
# get current wallpaper's pathprint(get_wallpaper()) # set your photoset_wallpaper("location/to/image.jpg")
Output:
D:\img\wallpapersden.com_money-heist_3840x2232.jpg
Background changer Application with Tkinter: This Script implements the above Implementation into a GUI.
Python3
# import modulesfrom tkinter import *from tkinter import filedialogfrom wallpaper import set_wallpaper # user define functiondef change_wall():       # set your photo    try:        set_wallpaper(str(path.get()))        check = "success"     except:         check = "Wallpaper not found !"    result.set(check)  def browseFiles():    filename = filedialog.askopenfilename(initialdir="/",                                          title="Select a File",                                          filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))    path.set(filename)         # Change label contents    label_file_explorer.configure(text="File Opened: "+filename)    return filename  # object of tkinter# and background set for redmaster = Tk()master.configure(bg='light grey') # Variable Classes in tkinterresult = StringVar()path = StringVar()  label_file_explorer = Label(    master, text="Select a image", width=100, fg="blue")  # Creating label for each information# name using widget LabelLabel(master, text="Select image : ", bg="light grey").grid(row=0, sticky=W)Label(master, text="Status :", bg="light grey").grid(row=3, sticky=W)  # Creating label for class variable# name using widget EntryLabel(master, text="", textvariable=result,      bg="light grey").grid(row=3, column=1, sticky=W) # creating a button using the widget# Button that will call the submit functionb = Button(master, text="Open", command=browseFiles, bg="white")b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5,) label_file_explorer.grid(column=1, row=1) c = Button(master, text="Apply", command=change_wall, bg="white")c.grid(row=2, column=2, columnspan=2, rowspan=2, padx=5, pady=5,) mainloop()
Output:
After selecting this wallpaper.
singghakshay
avtarkumar719
Python Tkinter-exercises
Python-tkinter
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python