我嘗試使用 ctypes 模塊在 Python 中獲取當前壁紙的路徑。但結果程式回傳值 1。
import ctypes
SPI_GETDESKWALLPAPER = 0x0073
path = ctypes.create_unicode_buffer(260)
a = ctypes.windll.user32.SystemParametersInfoW(SPI_GETDESKWALLPAPER, 100, path, 0)
print(a)
uj5u.com熱心網友回復:
該值在 中回傳path。 a在您的示例中是回傳碼(1=成功)。定義引數型別和回傳型別也是一個好習慣,因此ctypes在將引數從 Python 轉換為 C 時不必猜測,反之亦然。
import ctypes as ct
from ctypes import wintypes as w
SPI_GETDESKWALLPAPER = 0x0073
dll = ct.WinDLL('user32')
dll.SystemParametersInfoW.argtypes = w.UINT,w.UINT,w.LPVOID,w.UINT
dll.SystemParametersInfoW.restype = w.BOOL
path = ct.create_unicode_buffer(260)
result = dll.SystemParametersInfoW(SPI_GETDESKWALLPAPER, ct.sizeof(path), path, 0)
print(result, path.value)
uj5u.com熱心網友回復:
如果你愿意,試試這個代碼,也許它會幫助你:)
from os import path, getenv, getcwd
from ctypes import windll
import ctypes
from shutil import copyfile
from PIL import Image
from tempfile import NamedTemporaryFile
SPI_GETDESKWALLPAPER = 0x0073
path = ctypes.create_unicode_buffer(260)
returnImgObj = ctypes.windll.user32.SystemParametersInfoW(SPI_GETDESKWALLPAPER, 100, path, 0)
currentWallpaper = getenv(
'APPDATA') "\\Microsoft\\Windows\\Themes\\TranscodedWallpaper" #this is path manuel
if returnImgObj == True:
a= Image.open(currentWallpaper)
a.show()
print(str(currentWallpaper))
else:
tempFile = NamedTemporaryFile(mode="wb", suffix='.jpg').name
copyfile(currentWallpaper, tempFile)
a= Image.open(currentWallpaper)
a.show()
print(str(currentWallpaper))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/428289.html
標籤:Python python-3.x 视窗 温纳皮 类型
