I'm trying to do something simple like get calc.exe to start minimized, but it's not happening.

import subprocess
import win32gui
import win32con

info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = win32con.SW_SHOWMINIMIZED
x = subprocess.Popen("calc.exe", startupinfo = info)

It pops up the same as always, no matter what I provide for wShowWindow.

Dani AI

Generated

Short answer: STARTUPINFO.wShowWindow is only a suggestion to the new process — it becomes the default nCmdShow value used by the process the first time that process calls ShowWindow, but the child program can ignore or override it. That is why passing STARTF_USESHOWWINDOW/wShowWindow often has no visible effect if the target app decides its own window state. (learn.microsoft.com)

That matches what and saw: some apps (WordPad) honor the caller’s show flags, other apps (Calculator) do not. On modern Windows the Calculator may even be a Store/UWP-backed app launched by a stub, so the visible window can belong to a different process (CalculatorApp.exe) or be created in a way that ignores the STARTUPINFO you passed. That explains why both your STARTUPINFO and a plain start /MIN sometimes don’t minimize calc. (stackoverflow.com)

Practical options that work reliably:

  • Try ShellExecuteEx/ShellExecute with the nShow argument; it still depends on the app but is another common path. (learn.microsoft.com)

  • More reliable: start the process, wait for its top-level window(s), then call ShowWindow on that HWND. Example (pywin32):

import subprocess, time
import win32gui, win32con, win32process

p = subprocess.Popen(['calc.exe'])
pid = p.pid

def windows_for_pid(pid):
    res = []
    def cb(hwnd, lParam):
        try:
            _, wnd_pid = win32process.GetWindowThreadProcessId(hwnd)
        except Exception:
            return True
        if wnd_pid == lParam and win32gui.IsWindowVisible(hwnd):
            res.append(hwnd)
        return True
    win32gui.EnumWindows(cb, pid)
    return res

end = time.time() + 5.0
while time.time() < end:
    wins = windows_for_pid(pid)
    if wins:
        for hwnd in wins:
            win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
        break
    time.sleep(0.05)

This enumerates top-level windows, matches by process id, and minimizes when the window appears. (learn.microsoft.com)

Notes and troubleshooting: add a timeout and a small sleep loop because the UI may appear asynchronously; try SW_FORCEMINIMIZE if a normal minimize fails. If the spawned app is actually a UWP/store app or runs in another session, the HWND you need may be created by a different process — inspect running processes after launch to find the real window owner.

start /MIN calc

also does not start minimized from command line, so it must be problem with the program started, not the command. The wordpad starts minimized with your code and full path (as it is not in PATH in my computer).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.