简体   繁体   English

在python窗口化(Tkinter)应用程序中禁止子进程控制台输出

[英]Suppress subprocess console output in a python windowed (Tkinter) app

I am attempting to run the following code in a python app executable made using 我正在尝试使用python应用程序执行以下代码

pyinstaller -w -F script.py pyinstaller -w -F script.py

:

def ffmpeg_command(sec):
    cmd1 = ['ffmpeg', '-f','gdigrab','-framerate',config.get('FFMPEG_Settings','Framerate'),'-i','desktop',gen_filename_from_timestamp_and_extension()]


    proc = subprocess.Popen(cmd1,stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

    duration = sec
    sleeptime = 0
    while proc.poll() is None and sleeptime < duration: 
        # Wait for the specific duration or for the process to finish
        time.sleep(1)
        sleeptime += 1

    proc.terminate()

The above code is run when a Tkinter button is pressed and this code is called from the button click handler. 当按下Tkinter按钮并从按钮单击处理程序调用此代码时,将运行以上代码。

My problem is that when I am running the exe this doesn't run ffmpeg. 我的问题是,当我运行exe时,它不会运行ffmpeg。 However, If I set the command to be: 但是,如果我将命令设置为:

proc = subprocess.Popen(cmd1)

FFMPEG does run, I get the movie file I wanted but I can see the console window for FFMPEG. FFMPEG确实运行,我得到了想要的电影文件,但可以看到FFMPEG的控制台窗口。 So I end up getting the console window in my movie. 因此,我最终在电影中获得了控制台窗口。 (I take care of minimizing the Tkinter window in the button click handler) (我会尽量减少按钮单击处理程序中的Tkinter窗口)

My question is how do I suppress the console window and still have FFMPEG run the way I want it to? 我的问题是如何隐藏控制台窗口,并且仍然以我希望的方式运行FFMPEG? I looked at the following threads but couldn't make it work: How to hide output of subprocess in Python 2.7 , Open a program with python minimized or hidden 我查看了以下线程,但无法正常工作: 如何在Python 2.7中隐藏子进程的输出, 用python最小化或隐藏打开程序

Thank you 谢谢

Thank you @Stack and @eryksun! 谢谢@Stack和@eryksun! I changed to the following code: 我更改为以下代码:

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
cmd1 = ['ffmpeg', '-f','gdigrab','-framerate',config.get('FFMPEG_Settings','Framerate'),'-i','desktop',gen_filename_from_timestamp_and_extension()]  
proc = subprocess.Popen(cmd1,stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,startupinfo=startupinfo)

Achieved what I wanted. 实现了我想要的。 Indeed, as @eryksun suggested, only redirecting the output didn't do it and I had to use stdin=subprocess.DEVNULL as well to suppress all output. 确实,正如@eryksun所建议的那样,仅重定向输出并没有做到这一点,我还必须使用stdin=subprocess.DEVNULL来禁止所有输出。

That still left the console window visible but by setting the startupinfo as mentioned above, the console window was hidden. 这仍然留在控制台窗口中可见,但通过设置startupinfo如上所述,控制台窗口被隐藏。 Also verified that FFMPEG goes away when the time expires. 还验证了时间到期后FFMPEG消失了。

Thank you for your help! 谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM