首先安装moudle pywin32-219.win32-py2.7.exe
下载地址: http://pan.baidu.com/s/1qWBHgRE
Python实现代码;
import os, win32com.client
def runScreensaver():
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Desktop")
for objItem in colItems:
if objItem.ScreenSaverExecutable:
os.system(objItem.ScreenSaverExecutable + " /start")
break
该段程序只能够在Python的主进程中运行,在线程中起不起来,报如下错误:
com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
经查阅
http://docs.activestate.com/activepython/2.5/pywin32/pythoncom__CoInitialize_meth.html
在起线程之前需要调用
pythoncom.CoInitialize()
来初始化com模块,相应代码:
import pythoncom
pythoncom.CoInitialize()