2010-02-23 24 views
5

我想要做的是編寫一個腳本,它只能在進程列表中打開一個應用程序。意味着它將被「隱藏」。我甚至不知道它是否可能在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服務器。

任何想法?

回答

6

您應該使用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." 
+0

你碰巧知道Linux上的相應模塊? – helplessKirk 2016-03-31 09:26:47

0

如果什麼出現是一個終端,重定向進程的標準輸出。

+0

這不是一個終端。 – wtz 2010-02-23 17:02:25

1

目的是什麼?

,如果你想隱藏(無窗)過程在後臺工作,最好的辦法是寫一個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 
+1

我認爲這是在我想要的方面。 該腳本的目的是僅在啓動腳本時運行隱藏的程序(無窗口)。我想要隱藏的程序是一個專有的應用程序,它執行一些數字運算,並且用C語言編寫。Python基本上隱藏了它,並在我寫的一個非常好的簡約透明GUI窗口中獲取進度。沒什麼大不了問題是我不希望這個巨大的窗口在我的桌面上浮動,所以我想隱藏它。 – wtz 2010-02-23 17:02:03

+0

@wtzolt,這取決於,但對於長時間運行的後臺進程服務是最好的方式,無論如何,我添加了另一種解決方案來隱藏任何類型的窗口,所以它應該與您的應用程序一起工作 – 2010-02-24 03:00:06

+0

@wtz也許這是一個有點offtopic,但那裏它是一個整潔的小工具,名爲「Power Menu」http://www.abstractpath.com/powermenu/它將一個主要的* ix桌面的一些功能帶到了windows,例如。最小化到托盤或總是在頂部 – enthus1ast 2014-01-31 15:37:59

10

這很容易:)
的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() 
相關問題