Search code examples
wxpythonphantomjspy2exe

How to hide PhantomJS console when running a GUI app in Py2Exe?


I made a program in wxPython and used py2exe to compile the software. When using the software functions that use PhantomJS, first a permission box opens, requiring permission, then a black console box appears over my GUI. It's certainly not professional. For those of you not familiar with PhantomJS, PhantomJS is an .exe that Selenium uses.

  1. Is there anyway to hide the console from appearing when my customer is using my wxPython app? Perhaps something in the py2exe options?

  2. Is there a way to use PhantomJS without having to ask the user for permission? Or at least change "PhantomJS requesting permission" to "My-software requesting permission". My customers don't really know what PhantomJS is so they all submit a ticket asking about it.

Thanks!


Solution

  • i dont even know what PhantomJS is but if it's an executable this may help.

    I pipe to ffmpeg from wxPython app by using subprocess.Popen and set shell and creationflags option. Then I see no console for ffmpeg.

    import subprocess
    from win32process import CREATE_NO_WINDOW
    
    p = subprocess.Popen(
                cmdstring, 
                stdin=subprocess.PIPE,
                bufsize=-1, 
                shell=False,
                creationflags = CREATE_NO_WINDOW
                )
    

    (edit)

    Like you wrote, there are two issues here. One is about UAC which is probably difficult to get around completely as explained here https://stackoverflow.com/a/131092/566035. But because you are anyway packaging with py2exe, you could try this py2exe packaging option: https://stackoverflow.com/a/1445547/566035.

    windows = [{
        'script': "yourapp.py",
        'uac_info': "requireAdministrator",
    },]
    

    This line goes to the setup.py file of py2exe to package your application.

    With this approach, the user will be asked for UAC permission only once when starting your wxpython app. And UAC will say that your application is requesting the permission (not PhantomJS).

    Another is the console window which can be suppressed by CREATE_NO_WINDOW as I described above. To be more complete, I added an example that I took from http://phantomjs.org/quick-start.html

     phantomjs loadspeed.js http://www.google.com
    

    To do this command from wxpython app, one can for example write a method of wx.Frame like:

     def OnButton(self, event):
    
          cmdstring = ('phantomjs.exe', 'loadspeed.js', 'http://www.google.com')
          p = subprocess.Popen(
                cmdstring, 
                stdout=subprocess.PIPE,
                shell=False,
                creationflags = CREATE_NO_WINDOW
                )
    
         print p.stdout.read()  # to get the output from phantomjs.exe
    

    phantomjs.exe and loadspeed.js need to be in the system path or in the same folder. I did a test on my PC and it printed this as output.

     Loading time 719 msec