How to properly use PyExecJS in the GUI interface program developed by python

Click above blue word [ protocol analysis and restoration ] to follow us


" A little trick to solve the flickering black frame. "

In the process of using python development, it is inevitable to develop applications with interfaces, and often use js to complete some functions. For example, I developed a small application using python:

Made a stock review tool

In this, the interface UI developed using PTQT5, most of the functions in it are implemented using Python, but some small functions are implemented using js, and finally packaged into exe, using pyinstaller, which is very convenient, but there are also some piece of cake.

Our exe has an interface. Of course, we don’t want the black command line window to exist. If js is not used in the code implementation, it is easy to solve:

pyinstaller -F -w xxx.py

It looks like it.

However, if js is used, and we use the PyExecJS library to execute js, it will be a bit troublesome. When this library is not packaged, everything is normal, but after we package the exe, this library will execute js, let the black command line window flash, very annoying, obviously everything is running normally, but it just flashes the black window.

In addition, this library has stopped maintenance, so I can only find the reason myself.

We generally use PyExecJS to execute js as follows:

import execjs
ctx = execjs.compile(jsstr) 
out =ctx.call('v')

The reason why the black window of the pyinstaller packager flashes past is because the final execution function of the call is _exec_with_pipe(self, source) in a certain class in _external_runtime.py, and it is found directly:

def _exec_with_pipe(self, source):
    cmd = self._runtime._binary()
    p = None
    try:
        p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=self._cwd, universal_newlines=True)
        input = self._compile(source)
        if six.PY2:
            input = input.encode(sys.getfilesystemencoding())
        stdoutdata, stderrdata = p.communicate(input=input)
        ret = p.wait()
    finally:
        del p
    self._fail_on_non_zero_status(ret, stdoutdata, stderrdata)
    return stdoutdata

see it? The fifth line of the above function actually uses Popen to implement the function of executing js. This Popen, of course, has a black command line interface by default, and it is useless to set no command line interface when pyinstaller is packaged.

If you find the cause of the problem, then solve it. We know that Popen sets startupinfo to STARTF_USESHOWWINDOW to hide the command line interface window. Then we modify the _external_runtime.py file and add it:

def _exec_with_pipe(self, source):
    cmd = self._runtime._binary()
    p = None
    try:
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        p = Popen(cmd, startupinfo=startupinfo, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=self._cwd, universal_newlines=True)
        input = self._compile(source)
        if six.PY2:
            input = input.encode(sys.getfilesystemencoding())
        stdoutdata, stderrdata = p.communicate(input=input)
        ret = p.wait()
    finally:
        del p
    self._fail_on_non_zero_status(ret, stdoutdata, stderrdata)
    return stdoutdata

Try to use pyinstaller to package the GUI application that uses the PyExecJS library to execute js. Is everything normal and there is no annoying flashing black frame?

After solving this problem, the GUI application is much more upscale all of a sudden, and it is no longer a copycat.

Although the stock resumption tool developed last time was sent to a few friends, it is really fake. Now it has been updated. If you want to update it, please contact me.

If you have an idea, feel free to communicate.

Don't forget to click "Looking", "Like" and "Share"

The new rule, to receive tweets in time, you must first star the official account

Don't forget to star or you will miss out

Long press to follow and communicate all the time.

Guess you like

Origin blog.csdn.net/yeyiqun/article/details/119880896