Python 打包程序判断是否已经运行

时间:2023-03-08 22:24:13

代码如下:

 # -*- coding: UTF8 -*-
from win32com.client import Dispatch
import win32com
import sys, os
from PyQt4 import QtCore, QtGui class Logicpy(QtGui.QWidget):
def __init__(self):
super(Logicpy, self).__init__()
self.resize(100, 100)
mythis = os.path.basename(os.path.realpath(sys.argv[0])) # 获得当前文件的名字
exist = self.proc_exist(mythis)
if exist:
Ok = QtGui.QMessageBox.question(self, (u'提示'),(u'已经运行'), QtGui.QMessageBox.Yes)
if Ok == QtGui.QMessageBox.Yes:
exit()
# QtCore.QCoreApplication.quit()
# QtGui.qApp.quit # 退出 #判断该进程是否存在
def proc_exist(self, process_name):
is_exist = False
wmi = win32com.client.GetObject('winmgmts:')
processCodeCov = wmi.ExecQuery('select * from Win32_Process where name=\"%s\"' %(process_name))
if len(processCodeCov) > 2:
is_exist = True
return is_exist if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Logic = Logicpy()
Logic.show()
sys.exit(app.exec_())

效果:

Python 打包程序判断是否已经运行Python 打包程序判断是否已经运行