Here's a straightforward procedural approach to creating a window using the Windows API in Python:
python
import ctypes
import ctypes.wintypes as wintypes
# Constants
WS_OVERLAPPEDWINDOW = 0x00CF0000
CW_USEDEFAULT = 0x80000000
SW_SHOW = 5
WM_DESTROY = 0x0002
# 1. Define the window procedure
def window_procedure(hwnd, msg, wparam, lparam):
if msg == WM_DESTROY:
ctypes.windll.user32.PostQuitMessage(0)
return 0
return ctypes.windll.user32.DefWindowProcW(hwnd, msg, wparam, lparam)
# 2. Register the window class
wnd_class = wintypes.WNDCLASSW()
wnd_class.lpfnWndProc = ctypes.WINFUNCTYPE(ctypes.c_int, wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM)(window_procedure)
wnd_class.hInstance = ctypes.windll.kernel32.GetModuleHandleW(None)
wnd_class.lpszClassName = "MyWindowClass"
if not ctypes.windll.user32.RegisterClassW(ctypes.byref(wnd_class)):
raise ctypes.WinError()
# 3. Create the window
hwnd = ctypes.windll.user32.CreateWindowExW(
0, # Extended style
"MyWindowClass", # Class name
"My WinAPI Window", # Window title
WS_OVERLAPPEDWINDOW, # Style
CW_USEDEFAULT, CW_USEDEFAULT, # Position (x, y)
400, 300, # Width, height
None, # Parent window
None, # Menu
wnd_class.hInstance, # Instance handle
None # Additional data
)
if not hwnd:
raise ctypes.WinError()
# 4. Show and update the window
ctypes.windll.user32.ShowWindow(hwnd, SW_SHOW)
ctypes.windll.user32.UpdateWindow(hwnd)
# 5. Message loop
msg = wintypes.MSG()
while ctypes.windll.user32.GetMessageW(ctypes.byref(msg), None, 0, 0) > 0:
ctypes.windll.user32.TranslateMessage(ctypes.byref(msg))
ctypes.windll.user32.DispatchMessageW(ctypes.byref(msg))
User
get rid of acronyms as much as possible
Assistant
Simple Windows GUI in Python
Here's a clear version without technical acronyms where possible:
python
import ctypes
from ctypes import wintypes
# Style settings
window_style = 0x00CF0000 # Standard window with title bar, borders, etc.
default_position = 0x80000000 # Let Windows choose default position
show_window = 5 # Make the window visible
# Window message constants
window_destroy_message = 0x0002 # Sent when window is being closed
# This function handles window events like closing
def handle_window_events(window_handle, message, parameter1, parameter2):
if message == window_destroy_message:
ctypes.windll.user32.PostQuitMessage(0) # Tell the system we're closing
return 0
# Let Windows handle other messages normally
return ctypes.windll.user32.DefWindowProcW(window_handle, message, parameter1, parameter2)
# Set up our window type
window_class = wintypes.WNDCLASSW()
window_class.event_handler = ctypes.WINFUNCTYPE(
ctypes.c_int,
wintypes.HWND,
wintypes.UINT,
wintypes.WPARAM,
wintypes.LPARAM
)(handle_window_events)
window_class.program_instance = ctypes.windll.kernel32.GetModuleHandleW(None)
window_class.class_name = "MyWindowClass"
# Register our window type with Windows
if not ctypes.windll.user32.RegisterClassW(ctypes.byref(window_class)):
raise ctypes.WinError()
# Create the actual window
window_handle = ctypes.windll.user32.CreateWindowExW(
0, # No special styles
"MyWindowClass", # Our registered class name
"My Python Window", # Window title
window_style, # Window style
default_position, default_position, # Start position
640, 480, # Width and height
None, # No parent window
None, # No menu
window_class.program_instance, # Our program
None # No extra data
)
if not window_handle:
raise ctypes.WinError()
# Display the window
ctypes.windll.user32.ShowWindow(window_handle, show_window)
ctypes.windll.user32.UpdateWindow(window_handle)
# Keep the window running and responsive
current_message = wintypes.MSG()
while ctypes.windll.user32.GetMessageW(ctypes.byref(current_message), None, 0, 0) > 0:
ctypes.windll.user32.TranslateMessage(ctypes.byref(current_message))
ctypes.windll.user32.DispatchMessageW(ctypes.byref(current_message))
User
RegisterClass failed with error: 87
[Error]
Assistant
The error "RegisterClass failed with error: 87" means ERROR_INVALID_PARAMETER, which typically occurs when the WNDCLASS structure isn't properly filled out before registration. Let's fix this:
Corrected Code (Procedural Style)
python
import ctypes
from ctypes import wintypes
# Constants (renamed for clarity)
STANDARD_WINDOW_STYLE = 0x00CF0000 # Title bar, resize, minimize/maximize, close
LET_WINDOWS_DECIDE_POSITION = 0x80000000
MAKE_WINDOW_VISIBLE = 5
WINDOW_CLOSING_MESSAGE = 0x0002 # WM_DESTROY
# Window event handler
def window_event_handler(window_handle, message, param1, param2):
if message == WINDOW_CLOSING_MESSAGE:
ctypes.windll.user32.PostQuitMessage(0)
return 0
return ctypes.windll.user32.DefWindowProcW(window_handle, message, param1, param2)
# Set up window properties
window_class = wintypes.WNDCLASSW()
window_class.style = 0 # Important: Must set class style (often missing)
window_class.lpfnWndProc = ctypes.WINFUNCTYPE(
ctypes.c_int, wintypes.HWND, wintypes.UINT,
wintypes.WPARAM, wintypes.LPARAM
)(window_event_handler)
window_class.hInstance = ctypes.windll.kernel32.GetModuleHandleW(None)
window_class.lpszClassName = "MyWindowClass"
window_class.hCursor = ctypes.windll.user32.LoadCursorW(None, 32512) # IDC_ARROW
window_class.hbrBackground = 1 + 1 # COLOR_WINDOW + 1 (white background)
# Register window class
if not ctypes.windll.user32.RegisterClassW(ctypes.byref(window_class)):
error_code = ctypes.get_last_error()
raise ctypes.WinError(error_code)
# Create window
window_handle = ctypes.windll.user32.CreateWindowExW(
0,
"MyWindowClass",
"My Python Window",
STANDARD_WINDOW_STYLE,
LET_WINDOWS_DECIDE_POSITION, LET_WINDOWS_DECIDE_POSITION,
800, 600, # Width, height
None, None, window_class.hInstance, None
)
if not window_handle:
error_code = ctypes.get_last_error()
raise ctypes.WinError(error_code)
# Show window
ctypes.windll.user32.ShowWindow(window_handle, MAKE_WINDOW_VISIBLE)
ctypes.windll.user32.UpdateWindow(window_handle)
# Message processing loop
current_message = wintypes.MSG()
while ctypes.windll.user32.GetMessageW(ctypes.byref(current_message), None, 0, 0) > 0:
ctypes.windll.user32.TranslateMessage(ctypes.byref(current_message))
ctypes.windll.user32.DispatchMessageW(ctypes.byref(current_message))