2

I've compiled a little python program and it has this window open (cmd console) and I would like to make it a background task or just hide it from the user somehow.

Yes I am aware that I can compile my py files with --windowed, but that's off the table, as it is causes other problems.

I'm hoping for a solution in PowerShell, or Python. Any advice or suggestions are great as well.

And no, I cannot close it (it closes the application I want open)

the two windows in question:

2 Answers 2

1

Your two easiest options would be PowerShell and Windows Scripting Host.
If you want the windows scripting host version, please ask.

Powershell:
You use the Start-Process cmdlet with the flag -WindowStyle hidden.

For instance:
Start-Process -WindowStyle hidden -FilePath python.exe -ArgumentList "c:\my_py_path\my_py_file.py"

Remember, you will have no window to close. To pull the plug, you will have to kill the task.

0

I know nothing of Python and can't make out the images you linked to, but PowerShell can hide any window you can obtain the handle to if you use Add-Type to Pinvoke the ShowWindow() function:

Add-Type @'
using System;
using System.Runtime.InteropServices;

public class API {

    public enum SW : int {
        Hide            = 0,
        Normal          = 1,
        ShowMinimized   = 2,
        Maximize        = 3,
        ShowNoActivate  = 4,
        Show            = 5,
        Minimize        = 6,
        ShowMinNoActive = 7,
        ShowNA          = 8,
        Restore         = 9,
        Showdefault     = 10,
        Forceminimize   = 11
    }

    [DllImport("user32.dll")]
    public static extern int ShowWindow(IntPtr hwnd, SW nCmdShow);
}
'@

Copy, paste, and execute the above code in a PowerShell window (or script) and the window can then hide and show itself like so:

$ThisWindow = [System.Diagnostics.Process]::GetCurrentProcess().MainwindowHandle
[API]::ShowWindow($ThisWindow,'Hide')
sleep -Seconds 5
[API]::ShowWindow($ThisWindow,'Show')

The handles to other top-level windows can be obtained from Get-Process or Pinvoking the FindWindow() functions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .