0

i want to cascade window in pywin32 module. i use: win32gui.CascadeWindow() but rased an error:

AttributeError: module 'win32gui' has no attribute 'CascadeWindow'. how can i fix this?

AttributeError: module 'win32gui' has no attribute 'CascadeWindow'. Did you mean: 'CreateWindow'?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
xander karimi
  • 3
  • 1
  • 1
  • 2

1 Answers1

0

[MS.Learn]: CascadeWindows function (winuser.h) is not wrapped (exported) by the Win32GUI (or any other) module (part of [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions)).

An alternative would be to call the function "directly", via [Python.Docs]: ctypes - A foreign function library for Python.
Check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) before going further.

code00.py:

#!/usr/bin/env python

import ctypes as cts
import sys
from ctypes import wintypes as wts

#import win32api as wapi


MDITILE_SKIPDISABLED = 0x0002
MDITILE_ZORDER = 0x0004


def main(*argv):
    kernel32 = cts.WinDLL("Kernel32.dll")
    user32 = cts.WinDLL("User32.dll")

    GetLastError = kernel32.GetLastError
    GetLastError.argtypes = ()
    GetLastError.restype = wts.DWORD

    CascadeWindows = user32.CascadeWindows
    CascadeWindows.argtypes = (wts.HWND, wts.UINT, wts.LPRECT, wts.UINT, wts.LPVOID)
    CascadeWindows.restype = wts.WORD

    hwnd = 0  # Desktop window
    res = CascadeWindows(hwnd, MDITILE_ZORDER, None, 0, None)
    print("Result: {:d}".format(res))
    if res == 0:
        # Could call wapi.GetLastError() instead, and avoid all CTypes boilerplate (the 4 lines above) for this function
        print("GLE: {:d}".format(GetLastError()))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q074953594]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" code00.py
Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32

Result: 29

Done.

As a side note, I managed to mess up my windows on the desktop (luckily, I didn't have as many of them as I usually do, open :) ).



Update #0

I submitted [GitHub]: mhammond/pywin32 - Add CascadeWindows wrapper (merged to main on 230104).
Check [SO]: How to change username of job in print queue using python & win32print (@CristiFati's answer) (at the end) for possible ways to go further.

CristiFati
  • 38,250
  • 9
  • 50
  • 87