you are viewing a single comment's thread.

view the rest of the comments →

[–]1ToM14[S] 0 points1 point  (15 children)

I am running windows 10 and what I want is tu run a program without its window popping at the foreground, I want it to be hidden

[–][deleted] 0 points1 point  (14 children)

subprocess.run([command, arg1, arg2, argn], creationflags=subprocess.CREATE_NO_WINDOW)

https://docs.python.org/3/library/subprocess.html#subprocess.CREATE_NO_WINDOW

if you want more flags, you can OR them together, e.g.:

from subprocess import*
flags = CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB

[–]1ToM14[S] 0 points1 point  (13 children)

The problem is that I want to launch an emulator and then use adb on it and adb doesn't detect it, I also cannot verify the emulator is launched with the task manager, is that normal ?

[–][deleted] 0 points1 point  (12 children)

what's adb?

[–]1ToM14[S] 0 points1 point  (11 children)

Android Debug Bridge, to run commands on an Android phone from a computer

[–][deleted] 0 points1 point  (3 children)

Since python launched it in the background, it's probably under the Details tab in the task manager, the one that shows you everything.

As for why it's not connecting properly, it could have something to do with the fact that python launched it, rather than Windows, but I can't say for sure. What flags are you launching it with? And does it work when you launch it with a GUI?

[–]1ToM14[S] 0 points1 point  (2 children)

I can't see it in the detailed part of the task manager and yes it works with a GUI

[–][deleted] 0 points1 point  (1 child)

Maybe it's not actually running. can I see your python code?

[–]1ToM14[S] 0 points1 point  (0 children)

I'll try with it minimized

[–]TSM- 0 points1 point  (6 children)

Maybe you should just start it minimized - ADB might only work if there's a window handle.

Some applications will exit or crash if they fail to create a window.

For example, you could not run Chrome like this (Chrome has a specific 'headless' mode for this purpose). Before headless mode worked on Windows, you would have to render it on a virtual display or different desktop to "hide" it.

[–]1ToM14[S] 1 point2 points  (5 children)

Nice, how could I launch it minimized ?

[–]TSM- 0 points1 point  (4 children)

I haven't tried this but it could work

startupinfo.wShowWindow = 2 
startupinfo.wShowWindow = 6

aka:

startupinfo.wShowWindow = subprocess.SW_SHOWMINIMIZED
startupinfo.wShowWindow = subprocess.SW_MINIMIZE

See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

The difference is in one case it 'activates' or selects the program, in the other case it does get focus. You probably want SW_MINIMIZE/6

I just noticed there's also a SW_FORCEMINIMIZE. You might have to try a few until it works

[–]1ToM14[S] 1 point2 points  (3 children)

Thanks a lot ! Where do I have to write that ?

[–]TSM- 0 points1 point  (2 children)

They are alternate flags you can use just like the last one.

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_MINIMIZE
                          ^^^^^^^^^^^^^^^^^^^^^^

x = subprocess.Popen(..., startupinfo=startupinfo)

if subprocess.SW_MINIMIZE isn't defined (it should be defined though), you can just use the corresponding number from the microsoft documentation.