windows下PyQt 调用命令行避免弹出黑框的方法

一般在pycharm里面直接运行代码时候还好,但是通过pyinstaller打包成exe之后,再调用命令行下的命令时候,主界面背后会弹出来一个黑色框,影响使用体验。

关键的做法是在subprocess.Popen里面传一个startupinfo对象,其中指定wShowWindow属性。

具体参考python手册:

一个例子如下:

    def run_cmd(self):
        if self.cmd:
            st = subprocess.STARTUPINFO()
            st.dwFlags = subprocess.STARTF_USESHOWWINDOW
            st.wShowWindow = subprocess.SW_HIDE
            p = subprocess.Popen(self.cmd, stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 startupinfo=st)
            retcode = p.poll()
            while retcode is None:
                content = p.stdout.readline().decode("gbk")
                self.runStateChanged.emit(content)
                retcode = p.poll()
            self.runStateChanged.emit("ret code:%d" % retcode)
原文地址:https://www.cnblogs.com/leipei2352/p/15265098.html