如何使用Python更改桌面背景?

问题描述 投票:0回答:16

如何使用 python 更改桌面背景?

我想在 Windows 和 Linux 中都这样做。

python background desktop
16个回答
44
投票

对于Python3.5,SystemParametersInfoA 不起作用。 使用系统参数信息W。

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20, 0, "absolute path" , 0)

43
投票

在使用 python2.5 或更高版本的 Windows 上,使用 ctypes 加载 user32.dll 并通过 SPI_SETDESKWALLPAPER 操作调用

SystemParametersInfo()

例如:

import ctypes
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "image.jpg" , 0)

21
投票

我在我的一个初始项目中使用以下方法:

    def set_wallpaper(self,file_loc, first_run):
        # Note: There are two common Linux desktop environments where 
        # I have not been able to set the desktop background from 
        # command line: KDE, Enlightenment
        desktop_env = self.get_desktop_environment()
        try:
            if desktop_env in ["gnome", "unity", "cinnamon"]:
                uri = "'file://%s'" % file_loc
                try:
                    SCHEMA = "org.gnome.desktop.background"
                    KEY = "picture-uri"
                    gsettings = Gio.Settings.new(SCHEMA)
                    gsettings.set_string(KEY, uri)
                except:
                    args = ["gsettings", "set", "org.gnome.desktop.background", "picture-uri", uri]
                    subprocess.Popen(args)
            elif desktop_env=="mate":
                try: # MATE >= 1.6
                    # info from http://wiki.mate-desktop.org/docs:gsettings
                    args = ["gsettings", "set", "org.mate.background", "picture-filename", "'%s'" % file_loc]
                    subprocess.Popen(args)
                except: # MATE < 1.6
                    # From https://bugs.launchpad.net/variety/+bug/1033918
                    args = ["mateconftool-2","-t","string","--set","/desktop/mate/background/picture_filename",'"%s"' %file_loc]
                    subprocess.Popen(args)
            elif desktop_env=="gnome2": # Not tested
                # From https://bugs.launchpad.net/variety/+bug/1033918
                args = ["gconftool-2","-t","string","--set","/desktop/gnome/background/picture_filename", '"%s"' %file_loc]
                subprocess.Popen(args)
            ## KDE4 is difficult
            ## see http://blog.zx2c4.com/699 for a solution that might work
            elif desktop_env in ["kde3", "trinity"]:
                # From http://ubuntuforums.org/archive/index.php/t-803417.html
                args = 'dcop kdesktop KBackgroundIface setWallpaper 0 "%s" 6' % file_loc
                subprocess.Popen(args,shell=True)
            elif desktop_env=="xfce4":
                #From http://www.commandlinefu.com/commands/view/2055/change-wallpaper-for-xfce4-4.6.0
                if first_run:
                    args0 = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-path", "-s", file_loc]
                    args1 = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-style", "-s", "3"]
                    args2 = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-show", "-s", "true"]
                    subprocess.Popen(args0)
                    subprocess.Popen(args1)
                    subprocess.Popen(args2)
                args = ["xfdesktop","--reload"]
                subprocess.Popen(args)
            elif desktop_env=="razor-qt": #TODO: implement reload of desktop when possible
                if first_run:
                    desktop_conf = configparser.ConfigParser()
                    # Development version
                    desktop_conf_file = os.path.join(self.get_config_dir("razor"),"desktop.conf") 
                    if os.path.isfile(desktop_conf_file):
                        config_option = r"screens\1\desktops\1\wallpaper"
                    else:
                        desktop_conf_file = os.path.join(self.get_home_dir(),".razor/desktop.conf")
                        config_option = r"desktops\1\wallpaper"
                    desktop_conf.read(os.path.join(desktop_conf_file))
                    try:
                        if desktop_conf.has_option("razor",config_option): #only replacing a value
                            desktop_conf.set("razor",config_option,file_loc)
                            with codecs.open(desktop_conf_file, "w", encoding="utf-8", errors="replace") as f:
                                desktop_conf.write(f)
                    except:
                        pass
                else:
                    #TODO: reload desktop when possible
                    pass 
            elif desktop_env in ["fluxbox","jwm","openbox","afterstep"]:
                #http://fluxbox-wiki.org/index.php/Howto_set_the_background
                # used fbsetbg on jwm too since I am too lazy to edit the XML configuration 
                # now where fbsetbg does the job excellent anyway. 
                # and I have not figured out how else it can be set on Openbox and AfterSTep
                # but fbsetbg works excellent here too.
                try:
                    args = ["fbsetbg", file_loc]
                    subprocess.Popen(args)
                except:
                    sys.stderr.write("ERROR: Failed to set wallpaper with fbsetbg!\n")
                    sys.stderr.write("Please make sre that You have fbsetbg installed.\n")
            elif desktop_env=="icewm":
                # command found at http://urukrama.wordpress.com/2007/12/05/desktop-backgrounds-in-window-managers/
                args = ["icewmbg", file_loc]
                subprocess.Popen(args)
            elif desktop_env=="blackbox":
                # command found at http://blackboxwm.sourceforge.net/BlackboxDocumentation/BlackboxBackground
                args = ["bsetbg", "-full", file_loc]
                subprocess.Popen(args)
            elif desktop_env=="lxde":
                args = "pcmanfm --set-wallpaper %s --wallpaper-mode=scaled" % file_loc
                subprocess.Popen(args,shell=True)
            elif desktop_env=="windowmaker":
                # From http://www.commandlinefu.com/commands/view/3857/set-wallpaper-on-windowmaker-in-one-line
                args = "wmsetbg -s -u %s" % file_loc
                subprocess.Popen(args,shell=True)
            ## NOT TESTED BELOW - don't want to mess things up ##
            #elif desktop_env=="enlightenment": # I have not been able to make it work on e17. On e16 it would have been something in this direction
            #    args = "enlightenment_remote -desktop-bg-add 0 0 0 0 %s" % file_loc
            #    subprocess.Popen(args,shell=True)
            #elif desktop_env=="windows": #Not tested since I do not run this on Windows
            #    #From https://stackoverflow.com/questions/1977694/change-desktop-background
            #    import ctypes
            #    SPI_SETDESKWALLPAPER = 20
            #    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, file_loc , 0)
            #elif desktop_env=="mac": #Not tested since I do not have a mac
            #    #From https://stackoverflow.com/questions/431205/how-can-i-programatically-change-the-background-in-mac-os-x
            #    try:
            #        from appscript import app, mactypes
            #        app('Finder').desktop_picture.set(mactypes.File(file_loc))
            #    except ImportError:
            #        #import subprocess
            #        SCRIPT = """/usr/bin/osascript<<END
            #        tell application "Finder" to
            #        set desktop picture to POSIX file "%s"
            #        end tell
            #        END"""
            #        subprocess.Popen(SCRIPT%file_loc, shell=True)
            else:
                if first_run: #don't spam the user with the same message over and over again
                    sys.stderr.write("Warning: Failed to set wallpaper. Your desktop environment is not supported.")
                    sys.stderr.write("You can try manually to set Your wallpaper to %s" % file_loc)
                return False
            return True
        except:
            sys.stderr.write("ERROR: Failed to set wallpaper. There might be a bug.\n")
            return False

    def get_config_dir(self, app_name=APP_NAME):
        if "XDG_CONFIG_HOME" in os.environ:
            confighome = os.environ['XDG_CONFIG_HOME'] 
        elif "APPDATA" in os.environ: # On Windows
            confighome = os.environ['APPDATA'] 
        else:
            try:
                from xdg import BaseDirectory   
                confighome =  BaseDirectory.xdg_config_home
            except ImportError: # Most likely a Linux/Unix system anyway
                confighome =  os.path.join(self.get_home_dir(),".config")
        configdir = os.path.join(confighome,app_name)
        return configdir

    def get_home_dir(self):
        if sys.platform == "cygwin":
            home_dir = os.getenv('HOME')
        else:
            home_dir = os.getenv('USERPROFILE') or os.getenv('HOME')
        if home_dir is not None:
            return os.path.normpath(home_dir)
        else:
            raise KeyError("Neither USERPROFILE or HOME environment variables set.")

get_desktop_environment方法已发布在另一个线程中


14
投票

在 gnome 桌面上,您通常使用 gconf 来执行此操作,可以直接调用 gconftool 或使用 gconf python 模块。后者在unutbu给出的链接中。第一种方法可以这样完成。

import commands
command = "gconftool-2 --set /desktop/gnome/background/picture_filename --type string '/path/to/file.jpg'"
status, output = commands.getstatusoutput(command)  # status=0 if success

9
投票

在 gnome 中,最好直接使用 gconf 的 python 绑定:

import gconf
conf = gconf.client_get_default()
conf.set_string('/desktop/gnome/background/picture_filename','/path/to/filename.jpg')

5
投票

在 Windows 上,您需要使用 pywin32windows API 进行一些技巧,在“linux”上,答案将取决于正在运行的桌面 - KDE、Gnome 或更奇特的东西。在 KDE(也许还有 Gnome)下,您可以使用 D-Bus 发送消息,您可以使用命令行工具 dbus-send 来执行此操作,而无需包含任何新库。

另一个选项是将桌面壁纸设置为一个文件,然后从 python 中编辑/替换该文件 - 但这可能只会在用户登录时导致更改。


5
投票

首先,导入

ctypes
:它可以让你访问Windows组件,如屏保、壁纸等

那就打电话吧

ctypes.windll.user32.SystemParametersInfoA(20, 0, the_complete_path_of_your_image, 0)

确保路径是图像的完整路径,不仅仅是活动目录中的路径


4
投票

如果您在 64 位或 32 位操作系统上运行,则调用的 SystemParametersInfo 方法有所不同。对于 64 位,您必须使用 SystemParametersInfoW (Unicode);对于 32 位,您必须使用 SystemParametersInfoA (ANSI)

import struct
import ctypes


SPI_SETDESKWALLPAPER = 20
WALLPAPER_PATH = 'C:\\your_file_name.jpg'


def is_64_windows():
    """Find out how many bits is OS. """
    return struct.calcsize('P') * 8 == 64


def get_sys_parameters_info():
    """Based on if this is 32bit or 64bit returns correct version of SystemParametersInfo function. """
    return ctypes.windll.user32.SystemParametersInfoW if is_64_windows() \
        else ctypes.windll.user32.SystemParametersInfoA


def change_wallpaper():
    sys_parameters_info = get_sys_parameters_info()
    r = sys_parameters_info(SPI_SETDESKWALLPAPER, 0, WALLPAPER_PATH, 3)

    # When the SPI_SETDESKWALLPAPER flag is used,
    # SystemParametersInfo returns TRUE
    # unless there is an error (like when the specified file doesn't exist).
    if not r:
        print(ctypes.WinError())


change_wallpaper()

4
投票
import ctypes,win32con

def getWallpaper():
    ubuf = ctypes.create_unicode_buffer(512)
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_GETDESKWALLPAPER,len(ubuf),ubuf,0)
    return ubuf.value

def setWallpaper(path):
    changed = win32con.SPIF_UPDATEINIFILE | win32con.SPIF_SENDCHANGE
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_SETDESKWALLPAPER,0,path,changed)

或者:(使用

SystemParametersInfoA

def getWallpaper():
    sbuf = ctypes.create_string_buffer(512) # ctypes.c_buffer(512)
    ctypes.windll.user32.SystemParametersInfoA(win32con.SPI_GETDESKWALLPAPER,len(sbuf),sbuf,0)
    return sbuf.value

def setWallpaper(path):
    changed = win32con.SPIF_UPDATEINIFILE | win32con.SPIF_SENDCHANGE
    ctypes.windll.user32.SystemParametersInfoA(win32con.SPI_SETDESKWALLPAPER,0,path.encode(),changed) # "".encode() = b""

论据是:

SystemParametersInfo(SetOrGet, GetBufferSize, SetBufferOrGetBuffer, SetChange)

路径必须是绝对路径,因此如果您使用与脚本相关的路径,请执行以下操作:

path = os.path.abspath(path)

要了解更多可以使用

SystemParametersInfo
做的事情,请参阅 docs
(靠近底部有一个更改鼠标速度的示例)

附注这里已经有很多答案,但他们忽略了您应该做的广播。当然,没有它它也能工作,但不正确使用它是不好的做法。

P.P.S 他们只给出了硬编码值,而不是它们来自的变量。

另请注意,在获取路径时,我使用 512 个字符作为缓冲区大小,只是为了更安全,因为路径可能超过 256 个。不过,我怀疑是否有人会拥有那么长的路径。

还有一点要注意。我只在Python 3中测试了上述示例,但我不认为SystemParametersInfoA需要Python 2中的.encode()。(我相信他们将Python 3中的字符串更新为unicode)SystemParametersInfoW中的字符串可能需要转换为Python 2.


3
投票

我阅读了所有答案,经过一段时间的搜索,我找到了一个更简单的解决方案。

安装名为 py-wallpaper 的模块。

pip install py-wallpaper

导入模块。

from wallpaper import set_wallpaper, get_wallpaper

使用set walpaper设置壁纸

set_wallpaper("location/to/image.jpg")

使用 get wall 获取当前壁纸的路径

print(get_wallpaper())

谢谢。


2
投票

更改桌面背景图片

    import ctypes
    import os
    SPI_SETDESKWALLPAPER = 20 
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, 'your image path', 3) 
    #'C:\\Users\\Public\\Pictures\\abc.jpg'

它对我来说效果很好。 windows10,python27


2
投票

在Python2.5或更高版本的Windows上,使用ctypes加载user32.dll并调用

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20,0,"Path_wallpaper", 0) 
     speak("Background changed succesfully")

1
投票

只是在 ShivaGuntuku 的帖子中添加一点精度: 在 python 3 中,您应该将 SytemParametersInfoA 中的“A”替换为“W”。使用 python 3 更改 windows10 中的桌面背景的小例子:

import ctypes
import os
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoW(
    SPI_SETDESKWALLPAPER, 0, 'C:\\Users\\godet\\OneDrive\\Images\\breaker_wall.jpg', 0)

1
投票

你可以使用这个库PyWallpaper,在Mac上也为我工作。

要安装,请输入

pip install PyWallpaper
。 然后更改/设置您的壁纸 -

from PyWallpaper import change_wallpaper
change_wallpaper("/some_path/sample.jpg")

0
投票

这对我有用

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20,0,path:os.PathLike,3)

0
投票

结合那些使用aliae的人的一些答案,我为

创建了一个python oneliner别名
python -c "import pathlib; import ctypes; ctypes.windll.user32.SystemParametersInfoW(20, 0, str(pathlib.Path('$1').resolve()), 0)"
alias:
  - name: setwp
    value: |
      python -c "import pathlib; import ctypes; ctypes.windll.user32.SystemParametersInfoW(20, 0, str(pathlib.Path('$1').resolve()), 0)"
    type: function
    if: eq .OS "windows"
© www.soinside.com 2019 - 2024. All rights reserved.