代码如下:
1 # -*- coding: UTF8 -*- 2 from win32com.client import Dispatch 3 import win32com 4 import sys, os 5 from PyQt4 import QtCore, QtGui 6 7 class Logicpy(QtGui.QWidget): 8 def __init__(self): 9 super(Logicpy, self).__init__() 10 self.resize(100, 100) 11 mythis = os.path.basename(os.path.realpath(sys.argv[0])) # 获得当前文件的名字 12 exist = self.proc_exist(mythis) 13 if exist: 14 Ok = QtGui.QMessageBox.question(self, (u'提示'),(u'已经运行'), QtGui.QMessageBox.Yes) 15 if Ok == QtGui.QMessageBox.Yes: 16 exit() 17 # QtCore.QCoreApplication.quit() 18 # QtGui.qApp.quit # 退出 19 20 #判断该进程是否存在 21 def proc_exist(self, process_name): 22 is_exist = False 23 wmi = win32com.client.GetObject('winmgmts:') 24 processCodeCov = wmi.ExecQuery('select * from Win32_Process where name="%s"' %(process_name)) 25 if len(processCodeCov) > 2: 26 is_exist = True 27 return is_exist 28 29 30 31 if __name__ == "__main__": 32 app = QtGui.QApplication(sys.argv) 33 Logic = Logicpy() 34 Logic.show() 35 sys.exit(app.exec_())
效果: