[python-win32] win32 gui app example

Chris R. Martin bluedust at swbell.net
Fri Dec 10 02:14:38 CET 2004


I looked all over the 'net today and could not find any examples of how to build 
a Python application using the win32gui module. There are some examples in the 
distribution but they really cover how to write a application as a top-level 
window.

Using that code and some examples from win32 programming sites, I hacked 
together some code that almost works. The only problem that I have with it is 
that the program does not end after closing the main window. I am calling 
PostQuitMessage when the WM_DESTROY message is received, but this doesn't seem 
to work. I am assuming there is something wrong with my main loop. Can anyone help?

Thanks,
Chris


Code below
----------

#example1.py
import struct
import win32api
import win32con
import win32gui

class MainWindow:
    def __init__(self):
       win32gui.InitCommonControls()
       self.hinst = win32api.GetModuleHandle(None)
    def CreateWindow(self):
       className = self.RegisterClass()
       self.BuildWindow(className)

    def RegisterClass(self):
       className = "TeSt"
       message_map = {
          win32con.WM_DESTROY: self.OnDestroy,
       }
       wc = win32gui.WNDCLASS()
       wc.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
       wc.lpfnWndProc = message_map
       wc.cbWndExtra = 0
       wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW )
       wc.hbrBackground = win32con.COLOR_WINDOW + 1
       wc.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
       wc.lpszClassName = className
       # C code: wc.cbWndExtra = DLGWINDOWEXTRA + sizeof(HBRUSH) + 
(sizeof(COLORREF));
       wc.cbWndExtra = win32con.DLGWINDOWEXTRA + struct.calcsize("Pi")
       #wc.hIconSm = 0
       classAtom = win32gui.RegisterClass(wc)
       return className

    def BuildWindow(self, className):
       style = win32con.WS_OVERLAPPEDWINDOW
       xstyle = win32con.WS_EX_LEFT
       self.hwnd = win32gui.CreateWindow(className,
                             "ThisIsJustATest",
                             style,
                             win32con.CW_USEDEFAULT,
                             win32con.CW_USEDEFAULT,
                             500,
                             400,
                             0,
                             0,
                             self.hinst,
                             None)
       win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW)

    def OnDestroy(self, hwnd, message, wparam, lparam):
       win32gui.PostQuitMessage(0)
       return True

w = MainWindow()
w.CreateWindow()
win32gui.PumpMessages()

-------
End code


-- 
Guybrush: How can you see without eyeballs?
Murray: How can you walk around without a brain? Some things no one can
answer.


More information about the Python-win32 mailing list