2016-06-26 77 views
0

我試着SO和谷歌的每個例子,但他們沒有工作。我不知道爲什麼,劇本完成沒有任何錯誤。但背景圖像不會改變。我把這個圖像的絕對路徑,我試過jpg,png格式,基本上我嘗試了一切,但所有的例子完成沒有任何錯誤,但背景圖像並沒有改變。有沒有一個工作的例子呢? Windows的7的Python 3.4的Python 3.4更改桌面背景圖片不能正常工作

一些例子沒有工作;

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

#This example can't find images, but I put absolute path to it. Don't know what's the problem 
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() 
+0

你能顯示你的代碼嗎? –

+0

我試過10個例子,我應該把它們全部放在一起嗎?真? – GLHF

+0

好吧,你沒有提供太多的信息來獲得幫助。你能選擇一個你認爲應該工作的人嗎? –

回答

1

嘗試使用下面的代碼:

import struct 
import ctypes 
import os 

def is_64_windows(): 
    """Find out how many bits is OS. """ 
    return 'PROGRAMFILES(X86)' in os.environ 

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

SPI_SETDESKWALLPAPER = 20 
WALLPAPER_PATH = 'C:\\your_file_name.jpg' 
change_wallpaper() 

我覺得你的問題是,你有64個窗戶,但32 Python中,那麼你的is_64_windows()函數返回False但它實際上是True'PROGRAMFILES(X86)' in os.environ應該工作。

+0

哇。這實際上起作用。很多哇。希望我能多勞多得。 – GLHF