detecting boxes in windows using python

Stefan Quandt squan at web.de
Thu Sep 2 05:44:03 EDT 2004


bill.ramsay at clear.net.nz (bill ramsay) wrote in message news:<a1e5746a.0409011857.6dc229d3 at posting.google.com>...
I just wrote a comfortable solution for this purpose,
including logging to stdout.
The main functionality is in function ConfirmDialog()

Just copy code below to a python file and edit the PopupNames global
variable.

Prerequisite: You will have to install python windows extensions from
http://starship.python.net/crew/mhammond/

<code>
"""
Automatical confirmation of popup windows.
"""

from win32con import *
from win32api import *
from win32ui import *
from win32gui import GetClassName
from win32event import *
from time import time, ctime, sleep
import sys

# Captions (titles) of popup windows to confirm
# EDIT THIS
PopupNames = (
  'Error',
  'Information',
  'Install Dialler', # You better delete this line :-)
)

def GetWindowText( Window ):
  """
  Get text of all 'Static' elements of windows and return
concatenated.
  """
  Child, Text = None, ''
  while 1:
    try: Child = FindWindowEx( Window, Child, 'Static', None )
    except: break
    Text += '\n\t'.join( Child.GetWindowText().split( '\r' ) )
  return Text

def FindControl( Window, CName = 'OK', CType = 'Button' ):
  """
  Find control with name CName in Window
  
  @arg Window: Top level window
  @type Window: PyCWnd
  @arg CName: Control Name
  @type CName: string
  @arg CType: Control class
  @type CType: string
  @return Control
  @rtype: PyCwnd 
  """
  return FindWindowEx( Window, None, CType, CName )


def ConfirmDialog( Window, BName = None, Delay = 0.5 ):
  """
  Find button with name BName in Window and simulate a button
activation.
  
  @arg WName: Window Name
  @type WName: string
  @arg BName: Button Name
  @type BName: string
  @return: Button in case of success, negative error code else
  @rtype: PyCWnd
  """
  # Find Button
  Button = FindControl( Window, BName )  
  Button.SendMessage( BM_SETSTATE,  1, 0 )
  sleep( Delay )  # Window should show up at least for half a second.

  # Simulate button press to confirm window
  idButton = Button.GetDlgCtrlID()
  hButton = Button.GetSafeHwnd()
  Caption = Window.GetWindowText()
  Window.SendMessage( WM_COMMAND, MAKELONG( idButton, BN_CLICKED ),
hButton )

  print ctime( time() ), "Confirmed '%s'" %Caption
  return Button


if __name__ == '__main__':
  # Program parameters and options
  ConfDelay = 0.5
  if '-i' in sys.argv:
    ConfDelay = 0
    
  print ctime( time() ), "%s started" %sys.argv[ 0 ]
  
  while 1:
    for Name in PopupNames:
      try: Window = FindWindow( None, Name )
      except: continue

      # Extract text information from Popup (for logging).
      Message = GetWindowText( Window )  # Get message text before
window disappears

      ConfirmDialog( Window, 'OK', ConfDelay )
      
      if Message: print '\t' + Message
      break
    sleep( 1 )
</code>

Regards
  Stefan

> Dear all.
> 
> I am using an existing hodge-podge of an application that runs on top
> of an Access database.  This application dials up customer equipment, 
> handshakes then downloads/uploads various bits of information in the
> access database.
> 
> The original application is designed for some poor sod to sit there
> for hours and manually do the necessary.
> 
> I have automated various processes by 'back filling' the access
> database,  thereby fooling the application into doing what I want.
> 
> The problem that I have is that if the application comes across a busy
> signal, or if the call fails for what ever reason, it displays an
> error dialog box on the pc.  If there are a lot of these,  it causes
> the app to hang.
> 
> The underlying log files in the access database are still updated.
> 
> Is there a way in python to detect these dialog boxes,  then
> effectively close them?  either by pressing the boxes own close
> button, or the little cross thingy on the top right corner (i don't
> know what it is called).
> 
> The OS is win2k, latest service pack.
> 
> Any help that any of you can offer will be most greatfully received.
> 
> Kind regards
> 
> Bill



More information about the Python-list mailing list