Jayich-Lab / tray-launcher

A launcher for Windows that resides in the taskbar for managing .bat scripts.
MIT License
7 stars 1 forks source link

A `python xxx.py` script opens up a cmd window that does not close itself #8

Closed fanmingyu212 closed 2 years ago

fanmingyu212 commented 2 years ago

activate artiq && python "C:\Users\scientist\code\pydux\lib\control\clients\switched_ws8_client.py" creates a cmd window that won't close itself.

fanmingyu212 commented 2 years ago

activate artiq && start pythonw "C:\Users\scientist\code\pydux\lib\control\clients\switched_ws8_client.py" starts the wavemeter without the cmd window.

fanmingyu212 commented 2 years ago

Error message:

Traceback (most recent call last):
  File "C:\Users\scientist\Anaconda3\envs\artiq\lib\site-packages\tray_launcher\gui.py", line 179, in restart_script
    self.start_new_script(args[0][0])
  File "C:\Users\scientist\Anaconda3\envs\artiq\lib\site-packages\tray_launcher\gui.py", line 106, in start_new_script
    name = script_path.name
AttributeError: 'str' object has no attribute 'name'
danhuaxu commented 2 years ago

Ok, here is what I found: when we call python xxx.py, a new console window will always be created unless it is run from an existing console window, whereas when we do pythonw xxx.py, no console window will apear anyway. (Source: https://stackoverflow.com/questions/9705982/pythonw-exe-or-python-exe)

This explains what we saw: when the tray_launcher is opened with pythonw, no console window is brought up for it, so when it calls python ...py, a new console window shows up. On the other hand, if the tray launcher is opened with python, it is associated with this console window (so if you close this console window, the tray launcher goes away) even though no new console is created. In this case, calls to python ...py or pythonw ...py won't create new windows.

Seems there is no work around this. There must be at least one console window, unless all .bat scripts use pythonw instead of python. To be honest, this kind of defeats the purpose of this tray launcher?

I am more inclined to have one console window up, i.e. use python to invoke the tray launcher. This way there is at most one console window and it is not blocked -- might make it easier for later communicating with the tray launcher using cmd.

Thoughts?

fanmingyu212 commented 2 years ago

Would the creationflags keyword solve it? https://stackoverflow.com/a/25124003/4696323

danhuaxu commented 2 years ago

Would the creationflags keyword solve it? https://stackoverflow.com/a/25124003/4696323

That looks promising. I feel ashamed.

danhuaxu commented 2 years ago

creationflags argument does help.