用可执行文件隐藏 youtube-dl 的 window
hide youtube-dl's window with executable
我在 python 3.5 中的简化代码是:
...
command = 'youtube-dl -f bestaudio MYURL &'
outfile = open('test.txt','w')
call(command.split(), stdout=outfile, stderr=outfile)
...
我 运行 它来自 tkinter GUI。它在 Sublime Text 中工作正常,但是当我使我的脚本可执行时(通过 cx_freeze),youtube-dl 显示一个空的黑色 window 几秒钟。
它一直很完美,但是 window 很烦人。有办法隐藏吗?
stdout 在 GUI 应用程序中不起作用,您需要通过 win32api 设置正确的标志以隐藏子进程的 windows。 call()
的使用将不起作用,因为它不支持 STARTUPINFO 助手,您可以使用 Popen()
像这样应该可以解决问题:
from subprocess import Popen, STARTUPINFO, STARTF_USESHOWWINDOW, SW_HIDE, PIPE
si = STARTUPINFO()
si.dwFlags |= STARTF_USESHOWWINDOW
si.wShowWindow = SW_HIDE
proc = Popen(['youtube-dl', '-f', 'bestaudio', MYURL], stdin=PIPE,
stdout=stdout_file,
stderr=PIPE,
shell=False,
startupinfo=si)
ret_code = process.wait()
stdout_file.flush()
我在 python 3.5 中的简化代码是:
...
command = 'youtube-dl -f bestaudio MYURL &'
outfile = open('test.txt','w')
call(command.split(), stdout=outfile, stderr=outfile)
...
我 运行 它来自 tkinter GUI。它在 Sublime Text 中工作正常,但是当我使我的脚本可执行时(通过 cx_freeze),youtube-dl 显示一个空的黑色 window 几秒钟。
它一直很完美,但是 window 很烦人。有办法隐藏吗?
stdout 在 GUI 应用程序中不起作用,您需要通过 win32api 设置正确的标志以隐藏子进程的 windows。 call()
的使用将不起作用,因为它不支持 STARTUPINFO 助手,您可以使用 Popen()
像这样应该可以解决问题:
from subprocess import Popen, STARTUPINFO, STARTF_USESHOWWINDOW, SW_HIDE, PIPE
si = STARTUPINFO()
si.dwFlags |= STARTF_USESHOWWINDOW
si.wShowWindow = SW_HIDE
proc = Popen(['youtube-dl', '-f', 'bestaudio', MYURL], stdin=PIPE,
stdout=stdout_file,
stderr=PIPE,
shell=False,
startupinfo=si)
ret_code = process.wait()
stdout_file.flush()