用Python以最小化或隐藏方式打开程序

11 投票
5 回答
33683 浏览
提问于 2025-04-15 19:38

我想做的是写一个脚本,让一个应用程序只在进程列表中显示,也就是说它是“隐藏”的。我甚至不知道在Python中是否能做到这一点。

如果做不到,我也可以接受一个功能,允许用Python打开一个程序,并让它处于最小化状态,可能像这样:

import subprocess
def startProgram():
    subprocess.Hide(subprocess.Popen('C:\test.exe')) #  I know this is wrong but you get the idea...
startProgram()

有人建议使用win32com.client,但问题是我想启动的程序没有在名称下注册COM服务器。

有什么想法吗?

5 个回答

0

这个有什么用呢?

如果你想要一个隐藏的(没有窗口)后台进程,最好的办法就是写一个Windows服务,然后通过常规的Windows服务机制来启动和停止它。Windows服务可以很容易用Python编写,比如说这是我自己服务的一部分(不过没有一些修改的话是不能运行的)

import os
import time
import traceback

import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager

import jagteraho


class JagteRahoService (win32serviceutil.ServiceFramework):
    _svc_name_ = "JagteRaho"
    _svc_display_name_ = "JagteRaho (KeepAlive) Service"
    _svc_description_ = "Used for keeping important services e.g. broadband connection up"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.stop = False

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.log('stopping')
        self.stop = True

    def log(self, msg):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,msg))

    def SvcDoRun(self):
        self.log('folder %s'%os.getcwd())
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        self.start()

    def shouldStop(self):
        return self.stop

    def start(self):
        try:
            configFile = os.path.join(jagteraho.getAppFolder(), "jagteraho.cfg")
            jagteraho.start_config(configFile, self.shouldStop)
        except Exception,e:
            self.log(" stopped due to eror %s [%s]" % (e, traceback.format_exc()))
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

你可以通过下面的方式来安装它

python svc_jagteraho.py--startup auto install

然后可以通过下面的方式来运行它

python python svc_jagteraho.py start

我这个服务也会出现在服务列表里,比如说在services.msc中可以看到它,你可以在这里启动或停止它,另外你也可以使用命令行来操作

sc stop jagteraho
35

这很简单 :)
Python的Popen可以接受STARTUPINFO结构...
关于STARTUPINFO结构的详细信息可以查看这里:https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx

以隐藏方式运行:

import subprocess

def startProgram():
    SW_HIDE = 0
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_HIDE
    subprocess.Popen(r'C:\test.exe', startupinfo=info)
startProgram()

以最小化方式运行:

import subprocess

def startProgram():
    SW_MINIMIZE = 6
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_MINIMIZE
    subprocess.Popen(r'C:\test.exe', startupinfo=info)
startProgram()
8

你可以使用win32api来隐藏你的窗口,比如通过使用win32gui.EnumWindows这个方法,你可以列出所有的顶层窗口,然后把你的窗口隐藏起来。

下面是一个简单的例子,你可以这样做:

import subprocess
import win32gui
import time

proc = subprocess.Popen(["notepad.exe"])
# lets wait a bit to app to start
time.sleep(3)

def enumWindowFunc(hwnd, windowList):
    """ win32gui.EnumWindows() callback """
    text = win32gui.GetWindowText(hwnd)
    className = win32gui.GetClassName(hwnd)
    #print hwnd, text, className
    if text.find("Notepad") >= 0:
        windowList.append((hwnd, text, className))

myWindows = []
# enumerate thru all top windows and get windows which are ours
win32gui.EnumWindows(enumWindowFunc, myWindows)

# now hide my windows, we can actually check process info from GetWindowThreadProcessId
# http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx
for hwnd, text, className in myWindows:
    win32gui.ShowWindow(hwnd, False)

# as our notepad is now hidden
# you will have to kill notepad in taskmanager to get past next line
proc.wait()
print "finished."

撰写回答