如何在3.6环境中运行python 2.7代码

问题描述:

我想用python来设置我的Windows 10桌面图像。如何在3.6环境中运行python 2.7代码

import ctypes import os

SPI_SETDESKWALLPAPER = 20 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "C:\Users\dzure\Desktop\stuff\imgs\IMG_impr.JPG", 3) #'C:\\Users\\Public\\Pictures\\abc.jpg'

但是这个代码不工作3.6:当我在2.7中运行它下面的代码工作正常。我已经尝试使用2to3,但代码只是将桌面设置为纯黑色而不是图像。所以我想知道是否有办法将此代码放入python 3函数中,并将其视为python 2代码,或者只是一种在Windows 10中获得工作结果的方法。

好吧,我找到了我的回答。基本上我把这个代码:

reg add "HKCU\Control Panel\Desktop" /v Wallpaper /f /t REG_SZ /d C:\Users\dzure\Desktop\stuff\imgs\b23.bmp 

RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True 并提出一个.bat文件。然后,我拿了该文件,只是:

imp ort os 
q = 0 
while q < 10: 
      os.system('set.bat') 
      q += 1 

有时bat文件不起作用,所以这就是为什么我添加了while循环。