当前位置: 首页 > news >正文

给cmd控制台程序 套壳 美化

给cmd控制台程序套壳美化,可以获取程序的标准输出和报错信息。

# _*_ coding: utf-8 _*_
""" 控制台程序启动器,杜绝黑窗口。
Time:     2023/10/18 15:28
Author:   Jyun
Version:  V 0.1
File:     main.py
Blog:     https://ctrlcv.blog.csdn.net
"""
import os
import subprocess
import threading
import time
import tkinter as tk# 设置 Python 的标准输出编码[配置项]
os.environ['PYTHONIOENCODING'] = 'gbk'# 窗口标题[配置项]
TITLE_BAR_TEXT = "Demo v1.0"# 程序名称[配置项]
MAIN_TITLE = "Program Name"# 程序停止时的状态提示[配置项]
STOPPED_STATE_TEXT = "Program stopped."# 程序运行时的状态提示[配置项]
RUNNING_STATE_TEXT = "Program running."# 程序启动脚本(可以是打包后的exe程序)[配置项]
COMMAND = ['python', 'test_program.py']THREADS_LIST = []def async_way(func):def wrapper(*args, **kwargs):t = threading.Thread(target=func, args=args, kwargs=kwargs)t.setDaemon(True)t.start()THREADS_LIST.append(t)return t  # 返回线程对象用于后续操作return wrapperclass GUI(tk.Tk):def __init__(self):tk.Tk.__init__(self)self.process = Noneself.title(TITLE_BAR_TEXT)self.configure(bg="white")self.resizable(False, False)self.protocol("WM_DELETE_WINDOW", self.destroy)# 主标题self.label = tk.Label(self, text=MAIN_TITLE, font=("微软雅黑", 16), anchor="w", justify="left", bg="white")self.label.pack(fill="x", padx=25, pady=5)# 状态、操作栏块self.div = tk.Frame(self, bg="white")self.div.pack(fill="x", padx=25, pady=5)# 当前状态显示TXTself.state = tk.Label(self.div, text="Status Action Bar", font=("微软雅黑", 12), anchor="w", justify="left",bg="white")self.state.pack(side="left")# 日志显示区self.log_text = tk.Text(self, wrap=tk.WORD, width=60, height=20, bg="#f6f6f6", borderwidth=0, padx=10, pady=10)self.log_text.pack(padx=25, pady=(5, 25))# 停止按钮(如不需要 注释即可)self.stop_button = tk.Button(self.div, text="停止", width=10, command=self.stop, bd=0, bg="#ff8787")self.stop_button.pack(side="right")# 启动按钮(如不需要 注释即可)self.start_button = tk.Button(self.div, text="启动", width=10, command=self.start, bd=0, bg="#69db7c")self.start_button.pack(side="right")def log(self, message, color="black", autowrap=True):""" 输出日志:param message: 内容(行):param color: 文本颜色:param autowrap: 是否自动换行:return:"""message = str(message)if autowrap:message = message + "\n"self.log_text.tag_configure("custom_color", foreground=color)self.log_text.insert(tk.END, message, "custom_color")self.log_text.see(tk.END)# @async_waydef start(self):""" 启动子程序:return:"""self.state["text"] = RUNNING_STATE_TEXTif self.process is not None:self.log("程序正在运行,若要重新启动,请先停止", "red")returnself.process = subprocess.Popen(COMMAND, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)self.read_child_output()self.read_child_error()def stop(self):""" 停止子程序:return:"""# 程序正常运行时`self.process.poll()`为Noneif self.process is not None and self.process.poll() is None:self.process.terminate()self.log(f"等待进程结束:{self.process.pid}")self.wait_process_exit()else:self.process = None  # !self.state["text"] = STOPPED_STATE_TEXTself.log("程序已经停止")self.log(f'辅助线程: {len(THREADS_LIST)}')self.release_thread()@async_waydef read_child_output(self):""" 获取子进程的标准输出:return:"""while True:try:output = self.process.stdout.readline()except UnicodeDecodeError as e:self.log(f'主线程输出解码错误 UnicodeDecodeError: {e}', color="red")continueif not output:breakself.log(output, autowrap=False)@async_waydef read_child_error(self):""" 获取子进程的错误信息:return:"""while True:output = self.process.stderr.readline()if not output:breakself.log(output, color="red", autowrap=False)@async_waydef wait_process_exit(self):""" 等待进程结束:return:"""while self.process.poll() is None:time.sleep(1)self.log(f"进程已结束,退出代码为 {self.process.poll()}")self.process = Noneself.state["text"] = STOPPED_STATE_TEXTdef release_thread(self):""" 释放已结束的线程:return:"""for thread in THREADS_LIST:if not thread.is_alive():thread.join()THREADS_LIST.remove(thread)self.log(f"释放线程:{thread}")def run(self):self.mainloop()if __name__ == '__main__':gui = GUI()gui.run()# TODO: 待实现
#  启动后自动运行子程序
#  添加日志长度限制
#  添加标题栏图标
#  添加程序图标

GUI示例:
请添加图片描述

http://www.lryc.cn/news/197343.html

相关文章:

  • 【系统架构设计】架构核心知识: 1 构件和中间件
  • 通过开发者工具-网络排查响应时间过长的问题
  • 【Python】Python 实现 Excel 到 CSV 的转换程序
  • BUUCTF题解之[极客大挑战 2019]Havefun 1
  • DIV+CSS网页布局
  • python二次开发CATIA:CATIA Automation
  • 2023年中国云计算软件市场规模、市场结构及市场份额情况分析[图]
  • docker入门加实战—部署Java和前端项目
  • 机器人制作开源方案 | 行星探测车概述
  • Git基础命令
  • C#中Semaphore 和 CountdownEvent 的使用总结
  • THE PLANETS:EARTH vulnhub
  • 【随想】每日两题Day.13
  • CMake Cookbook
  • 钢铁异常分类 few-shot 问题 小陈读paper 钢铁2
  • flask实战(问答平台)
  • RK3568驱动模块编译进内核
  • 黑马程序员Java Web--14.综合案例--修改功能实现
  • 开源协议介绍
  • solidworks 2024新功能之-打造更加智能的工作 硕迪科技
  • Datawhale学习笔记AI +新能源:电动汽车充电站充电量预测
  • 记一次fineBI的增量删除更新BUG
  • rsync+inotify实时同步+双向同步
  • 7.继承与多态 对象村的优质生活
  • 机器视觉、图像处理和计算机视觉:概念和区别
  • 从零开始的C语言学习第二十课:数据在内存中的存储
  • 分布式内存计算Spark环境部署与分布式内存计算Flink环境部署
  • am权限系统对接笔记
  • 回首往昔,初学编程那会写过的两段愚蠢代码
  • 《Java面向对象程序设计》学习笔记——Java程序填空题