March 31, 2003
Driving win32 GUIs with Python, part 4

A correction...

Thomas Heller got in touch with me, and pointed out that my getMultipleWindowValues() function (see part 3) does A Very Bad Thing. It mutates a Python string. These are supposed to be immutable in Python. This didn't break anything of mine, or hasn't yet, but it certainly could. If, for example, a string being used as a key in a dictionary mutated, who knows what would happen? BANG!

Fixed code (using the array module) below. (Notice that I've also turned this function into a generator.)

def getMultipleWindowValues(hwnd, getCountMessage, getValueMessage):
bufferlength = struct.pack('i', 255)
count = win32gui.SendMessage(hwnd, getCountMessage, 0, 0)
for itemIndex in range(count):
value = array.array('c', bufferlength + str().ljust(253))
valueLength = win32gui.SendMessage(hwnd, getValueMessage, itemIndex, value)
yield value.tostring()[:valueLength]

Update: See the whole series: Driving win32 GUIs with Python, part 1, Driving win32 GUIs with Python, part 2, Driving win32 GUIs with Python, part 3, Driving win32 GUIs with Python, part 4 and 7 hours, one line of code.

Posted to Python by Simon Brunning at March 31, 2003 01:43 PM
Comments

You lucky sod. Simon's playing with the new Generator toys, which look fab and gro-o-o-ovy, and I've been writing a new application... in VB6. Talk about retro. Still, we eneded up having a chat about how OO VB6 *isn't*, and how for the designs I've had to do everything passing interfaces arround {VB6 has no Inheritance, only (a) non-inheritable user defined Classes and (b) user defined Interfaces}, and how you *can* do nearly all your OO designs just by passing interfaces arround, if you want a back-to-the-classroom lesson!

Posted by: Mark Matthews on March 31, 2003 02:00 PM

Hi Simon,
I saw your very interesting posting about "Driving win32 GUI with Python". I tried it and everything works fine. However, I tried to simulate a click on a menu item, but had no luck :-( How do you find the menu handle? I tried to do a ctypes.windll.user32.GetMenu(hwnd), but that crashes my python. Any idea ?

Many thanks,

Axel

Posted by: Axel Kowald on April 5, 2003 04:46 PM

These are very cool - you should put them all into an article somewhere - or at least create a static URL for the entire series (including future ones) and I'll link to it :)

win32gui.GetClassName() was just checked in ;)

Posted by: Mark Hammond on April 7, 2003 02:31 PM

I also meant to mention:

win32api.keybd_event() may be of interest.

Posted by: Mark Hammond on April 7, 2003 02:33 PM

With the revised definition above for getMultipleWindowValues(), each call to next() on the resulting generator gives me the same result...strange. This is w/ Python 2.3 on Windows XP SP1.

I rewrote the function to not be a generator, incorporating the information about using the array module (so the function just returns a list). I'd post the code here, but the indentation seems to vanish (-;

Posted by: Commenter on September 25, 2003 10:16 AM

Great stuff. I got it to work on Windows. Has anyone got it to work on Linux with Wine? I've got to the point where I can run the default notepad script (the imports work), but it bombs when it tries to find a window of class "Notepad". I tried the stuff from Post #1 and the list of top level windows is empty (even though I open Notepad successfully from inside my script!). Any help would be much appreciated.

Posted by: Brian Edwards on March 11, 2004 03:52 PM

Sorry, I've never touched Wine.

Well, not the software. I've had a glass or two of the beverage in my time. ;-)

Posted by: Simon Brunning on March 11, 2004 03:55 PM

I never drink... Wine.

(BTW, is there any reason you're using str().ljust(253) rather than " "*253?)

Posted by: Bela Lugosi on March 20, 2004 11:16 AM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?