python window批量启动程序

1.首先编写一个启动编辑文件的脚本,存放需要启动的软件路径:

# -*- coding: utf-8 -*-
import sys
import os
import threading
import time
import win32process

"""
CreateProcess(appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)

      appName         可执行文件名

      cmdLine         命令行参数

      procAttr        进程安全属性

      threadAttr      线程安全属性

      InheritHandle  继承标志

      CreationFlags  创建标志

      currentDir      进程的当前目录

      Attr             创建程序的属性
"""

dir_path = 'd:/one-button-start'
file_path = 'd:/one-button-start/start_app.txt'
app_list = []

class OneButton:
    # 定义构造器
    def __init__(self, app_list):
        # 封装账户编号、账户余额的两个成员变量
        self.app_list = app_list
        self.lock = threading.RLock()
    def getAppList(self):
        global app_list
        print("获取需要开机启动的软件")
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        file = open(file_path)
        while 1:
            lines = file.readlines(100000)
            if not lines:
                break
            index = 0
            for line in lines:
                lineStr = str(line).replace('\n', '')
                app_list.insert(index,lineStr)
                index = index + 1
        file.close()
        print('getAppList:↓↓↓')
        print(app_list)
        self.app_list = app_list
    def openAppFile(self):
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        if not os.path.exists(file_path):
            f = open(file_path, 'w', encoding="utf-8")
            f.close()
        print("打开文件,写入可执行程序文件路径,写完保存文件后启动程序")
        # 用记事本打开aa.txt
        os.system('notepad ' + file_path)  # aa.txt文件必须在当前程序目录
    def runApps(self):
        print('runApps:↓↓↓')
        print(self.app_list)
        if self.app_list and len(self.app_list) > 0:
            for app in self.app_list:
                print('run app path in:' + app)
                # 打开程序,获得其句柄
                try:
                    handle = win32process.CreateProcess(app, '', None, None, 0, win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO())
                except Exception as exc:
                    print('启动'+ app + '报错如下:↓↓↓')
                    print(exc)
                    print('请重新输入正确的路径')
                finally:
                    time.sleep(4)
        else:
            print("没有可以运行的程序!请录入可启动的程序")
            sys.exit(0)
    def main(self):
        self.getAppList()
        if self.app_list and len(self.app_list) > 0:
            self.runApps()
    def sotpApps(self):
        print("停止程序运行")
        # todo 关闭运行中的程序
        self.getAppList()
        if self.app_list and len(self.app_list) > 0:
            self.runApps()


if __name__ == '__main__':
    app = OneButton([])
    print('录入需要启动的程序')
    app.openAppFile()
    # 打包成exe程序
    # pyinstaller -F -w one_button_write.py
    # pyinstaller -F one_button_write.py  --noconsole
    # 打包成带图标的exe   # my.ico 是一个图标名,和当前的shjys_rjjqk.py文件在同一个目录下
    # pyinstaller -F  --icon=edit.ico one_button_write.py

2.读取脚本,一键启动

# -*- coding: utf-8 -*-
import sys
import os
import threading
import time
import win32process

"""
CreateProcess(appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)

      appName         可执行文件名

      cmdLine         命令行参数

      procAttr        进程安全属性

      threadAttr      线程安全属性

      InheritHandle  继承标志

      CreationFlags  创建标志

      currentDir      进程的当前目录

      Attr             创建程序的属性
"""


dir_path = 'd:/one-button-start'
file_path = 'd:/one-button-start/start_app.txt'
app_list = []

class OneButton:
    # 定义构造器
    def __init__(self, app_list):
        # 封装账户编号、账户余额的两个成员变量
        self.app_list = app_list
        self.lock = threading.RLock()
    def getAppList(self):
        global app_list
        print("获取需要开机启动的软件")
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        file = open(file_path)
        while 1:
            lines = file.readlines(100000)
            if not lines:
                break
            index = 0
            for line in lines:
                lineStr = str(line).replace('\n', '')
                app_list.insert(index,lineStr)
                index = index + 1
        file.close()
        print('getAppList:↓↓↓')
        print(app_list)
        self.app_list = app_list
    def openAppFile(self):
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        if not os.path.exists(file_path):
            f = open(file_path, 'w', encoding="utf-8")
            f.close()
        print("打开文件,写入可执行程序文件路径,写完保存文件后启动程序")
        # 用记事本打开aa.txt
        os.system('notepad ' + file_path)  # aa.txt文件必须在当前程序目录
    def runApps(self):
        print('runApps:↓↓↓')
        print(self.app_list)
        if self.app_list and len(self.app_list) > 0:
            for app in self.app_list:
                print('run app path in:' + app)
                # 打开程序,获得其句柄
                try:
                    handle = win32process.CreateProcess(app, '', None, None, 0, win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO())
                except Exception as exc:
                    print('启动'+ app + '报错如下:↓↓↓')
                    print(exc)
                    print('请重新输入正确的路径')
                finally:
                    time.sleep(4)
        else:
            print("没有可以运行的程序!请录入可启动的程序")
            sys.exit(0)
    def main(self):
        self.getAppList()
        if self.app_list and len(self.app_list) > 0:
            self.runApps()
    def sotpApps(self):
        print("停止程序运行")
        # todo 关闭运行中的程序
        self.getAppList()
        if self.app_list and len(self.app_list) > 0:
            self.runApps()


if __name__ == '__main__':
    app = OneButton([])
    # 2.一键启动运行程序
    print('一键启动运行程序')
    app.main()
    # 打包成exe程序
    # pyinstaller -F -w one_button_start.py
    #打包成带图标的exe  # my.ico 是一个图标名,和当前的shjys_rjjqk.py文件在同一个目录下
    # pyinstaller -F --icon=start.ico one_button_start.py

最后打包成exe文件

使用  pyinstaller  去打包

最后生成一下两个文件

 

使用方法:

首先点击一键编辑会跳出:写完之后保存关闭即可

 

 然后点击 one_button_start.exe即可启动相关软件

 

 

 exe下载地址:

链接:https://pan.baidu.com/s/1IUwH4E6l7QeTEMU-eIeNXg
提取码:kr0t

写的比较简单,大家不喜勿喷哈,很鸡肋仅供学习使用;

 

posted @ 2021-02-05 21:41  进击小螺号  阅读(572)  评论(0编辑  收藏  举报