標籤:
代碼如下:
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 = False23 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 = True27 return is_exist28 29 30 31 if __name__ == "__main__":32 app = QtGui.QApplication(sys.argv)33 Logic = Logicpy()34 Logic.show()35 sys.exit(app.exec_())
效果:
Python 打包程式判斷是否已經運行