欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python創(chuàng)建exe運(yùn)行器和截圖工具的示例詳解

 更新時(shí)間:2024年10月21日 10:48:20   作者:winfredzhang  
本文我們將探討如何使用Python和wxPython創(chuàng)建一個(gè)強(qiáng)大而實(shí)用的桌面應(yīng)用程序,可以遍歷指定文件夾中的所有EXE文件,感興趣的小伙伴可以了解一下

在今天的博客中,我們將探討如何使用Python和wxPython創(chuàng)建一個(gè)強(qiáng)大而實(shí)用的桌面應(yīng)用程序。這個(gè)應(yīng)用程序可以遍歷指定文件夾中的所有EXE文件,并提供運(yùn)行這些文件和自動(dòng)截圖的功能。無論你是系統(tǒng)管理員、軟件測(cè)試人員,還是僅僅對(duì)自動(dòng)化工具感興趣的開發(fā)者,這個(gè)項(xiàng)目都會(huì)給你帶來啟發(fā)。

C:\pythoncode\new\runfolderexeandsnapshot.py

功能概述

我們的應(yīng)用程序具有以下主要功能:

  • 選擇文件夾并遍歷其中的所有EXE文件(包括子文件夾)
  • 在列表框中顯示找到的EXE文件
  • 雙擊列表項(xiàng)運(yùn)行對(duì)應(yīng)的EXE文件
  • 一鍵運(yùn)行所有找到的EXE文件
  • 自動(dòng)為每個(gè)運(yùn)行的程序截圖
  • 將截圖以原EXE文件名保存在指定位置

技術(shù)棧

為了實(shí)現(xiàn)這些功能,我們使用了以下Python庫:

  • wxPython: 用于創(chuàng)建圖形用戶界面
  • os: 用于文件系統(tǒng)操作
  • subprocess: 用于運(yùn)行外部程序
  • pyautogui: 用于屏幕截圖
  • PIL (Python Imaging Library): 用于圖像處理

全部代碼

import wx
import os
import subprocess
import pyautogui
from PIL import Image

class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='EXE Runner and Screenshot Tool')
        panel = wx.Panel(self)
        
        self.list_box = wx.ListBox(panel, pos=(5, 35), size=(350, 300), style=wx.LB_SINGLE)
        self.list_box.Bind(wx.EVT_LISTBOX_DCLICK, self.on_dclick)
        
        select_btn = wx.Button(panel, label='選擇文件夾', pos=(5, 5), size=(100, 25))
        select_btn.Bind(wx.EVT_BUTTON, self.on_select)
        
        run_all_btn = wx.Button(panel, label='運(yùn)行全部', pos=(110, 5), size=(100, 25))
        run_all_btn.Bind(wx.EVT_BUTTON, self.on_run_all)
        
        self.SetSize((360, 375))
        self.Centre()
        
    def on_select(self, event):
        dlg = wx.DirDialog(self, "選擇一個(gè)文件夾", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.folder_path = dlg.GetPath()
            self.exe_files = self.get_exe_files(self.folder_path)
            self.list_box.Set(self.exe_files)
        dlg.Destroy()
        
    def get_exe_files(self, folder):
        exe_files = []
        for root, dirs, files in os.walk(folder):
            for file in files:
                if file.endswith('.exe'):
                    exe_files.append(os.path.join(root, file))
        return exe_files
        
    def on_dclick(self, event):
        index = event.GetSelection()
        self.run_exe(self.exe_files[index])
        
    def on_run_all(self, event):
        for exe in self.exe_files:
            self.run_exe(exe)
            
    def run_exe(self, exe_path):
        try:
            subprocess.Popen(exe_path)
            wx.Sleep(2)  # 等待程序啟動(dòng)
            self.take_screenshot(exe_path)
        except Exception as e:
            wx.MessageBox(f"運(yùn)行 {exe_path} 時(shí)出錯(cuò): {str(e)}", "錯(cuò)誤", wx.OK | wx.ICON_ERROR)
            
    def take_screenshot(self, exe_path):
        screenshot = pyautogui.screenshot()
        exe_name = os.path.splitext(os.path.basename(exe_path))[0]
        screenshot_folder = os.path.join(os.path.dirname(exe_path), "screenshot")
        if not os.path.exists(screenshot_folder):
            os.makedirs(screenshot_folder)
        screenshot_path = os.path.join(screenshot_folder, f"{exe_name}.png")
        screenshot.save(screenshot_path)

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

代碼解析

讓我們深入了解代碼的關(guān)鍵部分:

1. 用戶界面

我們使用wxPython創(chuàng)建了一個(gè)簡(jiǎn)潔的用戶界面,包含一個(gè)列表框和兩個(gè)按鈕:

class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='EXE Runner and Screenshot Tool')
        panel = wx.Panel(self)
        
        self.list_box = wx.ListBox(panel, pos=(5, 35), size=(350, 300), style=wx.LB_SINGLE)
        self.list_box.Bind(wx.EVT_LISTBOX_DCLICK, self.on_dclick)
        
        select_btn = wx.Button(panel, label='選擇文件夾', pos=(5, 5), size=(100, 25))
        select_btn.Bind(wx.EVT_BUTTON, self.on_select)
        
        run_all_btn = wx.Button(panel, label='運(yùn)行全部', pos=(110, 5), size=(100, 25))
        run_all_btn.Bind(wx.EVT_BUTTON, self.on_run_all)

2. 文件遍歷

我們使用os.walk()來遞歸遍歷文件夾及其子文件夾,查找所有的EXE文件:

def get_exe_files(self, folder):
    exe_files = []
    for root, dirs, files in os.walk(folder):
        for file in files:
            if file.endswith('.exe'):
                exe_files.append(os.path.join(root, file))
    return exe_files

3. 運(yùn)行EXE文件

我們使用subprocess.Popen()來運(yùn)行EXE文件:

def run_exe(self, exe_path):
    try:
        subprocess.Popen(exe_path)
        wx.Sleep(2)  # 等待程序啟動(dòng)
        self.take_screenshot(exe_path)
    except Exception as e:
        wx.MessageBox(f"運(yùn)行 {exe_path} 時(shí)出錯(cuò): {str(e)}", "錯(cuò)誤", wx.OK | wx.ICON_ERROR)

4. 屏幕截圖

我們使用pyautogui來捕獲屏幕截圖,并使用PIL來保存圖像:

def take_screenshot(self, exe_path):
    screenshot = pyautogui.screenshot()
    exe_name = os.path.splitext(os.path.basename(exe_path))[0]
    screenshot_folder = os.path.join(os.path.dirname(exe_path), "screenshot")
    if not os.path.exists(screenshot_folder):
        os.makedirs(screenshot_folder)
    screenshot_path = os.path.join(screenshot_folder, f"{exe_name}.png")
    screenshot.save(screenshot_path)

潛在應(yīng)用場(chǎng)景

這個(gè)工具可以在多種場(chǎng)景下發(fā)揮作用:

軟件測(cè)試: 自動(dòng)運(yùn)行多個(gè)程序并捕獲截圖,可以大大提高測(cè)試效率。

系統(tǒng)管理: 快速查看和運(yùn)行系統(tǒng)中的可執(zhí)行文件,對(duì)系統(tǒng)管理員很有幫助。

軟件開發(fā): 在開發(fā)過程中快速測(cè)試和記錄多個(gè)可執(zhí)行文件的運(yùn)行狀態(tài)。

教育: 作為一個(gè)教學(xué)工具,展示如何使用Python創(chuàng)建實(shí)用的桌面應(yīng)用程序。

改進(jìn)空間

雖然這個(gè)工具已經(jīng)相當(dāng)實(shí)用,但仍有一些可以改進(jìn)的地方:

  • 添加進(jìn)度條,顯示當(dāng)前正在運(yùn)行的程序和截圖進(jìn)度。
  • 實(shí)現(xiàn)更靈活的截圖時(shí)間控制,因?yàn)椴煌绦蚩赡苄枰煌膯?dòng)時(shí)間。
  • 添加選項(xiàng)來自動(dòng)關(guān)閉運(yùn)行的程序。
  • 實(shí)現(xiàn)更robust的錯(cuò)誤處理和日志記錄機(jī)制。
  • 添加對(duì)非EXE文件的支持,如批處理文件或其他可執(zhí)行文件。

運(yùn)行結(jié)果

到此這篇關(guān)于Python創(chuàng)建exe運(yùn)行器和截圖工具的示例詳解的文章就介紹到這了,更多相關(guān)Python創(chuàng)建exe運(yùn)行器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論