Windows dialog box removal

Discussion in 'Python' started by Dave Brueck, Sep 24, 2003.

  1. Dave Brueck

    Dave Brueck Guest

    On Thursday 25 September 2003 06:18 am, SectorUnknown wrote:
    > I've written a Python script that runs a MS Windows program using popen.
    > However, at the end of the Windows program, a dialog box appears and
    > asks for the user to click on OK.
    >
    > Is there a way to have Python set focus on the dialog box and click OK
    > so my script can continue?
    >
    > Does anyone have an example?


    I wouldn't be surprised if there's an easier or more correct way to do this,
    but if you can reliably know the title of the window then the code below
    should work (assuming you have ctypes installed):

    from ctypes import *
    user32 = windll.user32

    EnumWindowsProc = WINFUNCTYPE(c_int, c_int, c_int)

    def GetHandles(title, parent=None):
    'Returns handles to windows with matching titles'
    hwnds = []
    def EnumCB(hwnd, lparam, match=title.lower(), hwnds=hwnds):
    title = c_buffer(' ' * 256)
    user32.GetWindowTextA(hwnd, title, 255)
    if title.value.lower() == match:
    hwnds.append(hwnd)

    if parent is not None:
    user32.EnumChildWindows(parent, EnumWindowsProc(EnumCB), 0)
    else:
    user32.EnumWindows(EnumWindowsProc(EnumCB), 0)
    return hwnds

    Here's an example of calling it to click the Ok button on any window that has
    the title "Downloads properties" (most likely there are 0 or 1 such windows):

    for handle in GetHandles('Downloads properties'):
    for childHandle in GetHandles('ok', handle):
    user32.SendMessageA(childHandle, 0x00F5, 0, 0) # 0x00F5 = BM_CLICK

    -Dave
    Dave Brueck, Sep 24, 2003
    #1
    1. Advertising

  2. I've written a Python script that runs a MS Windows program using popen.
    However, at the end of the Windows program, a dialog box appears and
    asks for the user to click on OK.

    Is there a way to have Python set focus on the dialog box and click OK
    so my script can continue?

    Does anyone have an example?
    SectorUnknown, Sep 25, 2003
    #2
    1. Advertising

  3. Dave Brueck <> writes:

    > On Thursday 25 September 2003 06:18 am, SectorUnknown wrote:
    >> I've written a Python script that runs a MS Windows program using popen.
    >> However, at the end of the Windows program, a dialog box appears and
    >> asks for the user to click on OK.
    >>
    >> Is there a way to have Python set focus on the dialog box and click OK
    >> so my script can continue?
    >>
    >> Does anyone have an example?

    >
    > I wouldn't be surprised if there's an easier or more correct way to do this,
    > but if you can reliably know the title of the window then the code below
    > should work (assuming you have ctypes installed):

    [example snipped]

    I've added this example to the ctypes wiki, so it will not be lost. Hope
    that's ok ;-)

    <http://starship.python.net/crew/theller/moin.cgi/CtypesLinks>

    Thomas
    Thomas Heller, Sep 25, 2003
    #3
  4. That works great.

    Thanks!!!

    In article <>,
    says...
    > On Thursday 25 September 2003 06:18 am, SectorUnknown wrote:
    > > I've written a Python script that runs a MS Windows program using popen.
    > > However, at the end of the Windows program, a dialog box appears and
    > > asks for the user to click on OK.
    > >
    > > Is there a way to have Python set focus on the dialog box and click OK
    > > so my script can continue?
    > >
    > > Does anyone have an example?

    >
    > I wouldn't be surprised if there's an easier or more correct way to do this,
    > but if you can reliably know the title of the window then the code below
    > should work (assuming you have ctypes installed):
    >
    > from ctypes import *
    > user32 = windll.user32
    >
    > EnumWindowsProc = WINFUNCTYPE(c_int, c_int, c_int)
    >
    > def GetHandles(title, parent=None):
    > 'Returns handles to windows with matching titles'
    > hwnds = []
    > def EnumCB(hwnd, lparam, match=title.lower(), hwnds=hwnds):
    > title = c_buffer(' ' * 256)
    > user32.GetWindowTextA(hwnd, title, 255)
    > if title.value.lower() == match:
    > hwnds.append(hwnd)
    >
    > if parent is not None:
    > user32.EnumChildWindows(parent, EnumWindowsProc(EnumCB), 0)
    > else:
    > user32.EnumWindows(EnumWindowsProc(EnumCB), 0)
    > return hwnds
    >
    > Here's an example of calling it to click the Ok button on any window that has
    > the title "Downloads properties" (most likely there are 0 or 1 such windows):
    >
    > for handle in GetHandles('Downloads properties'):
    > for childHandle in GetHandles('ok', handle):
    > user32.SendMessageA(childHandle, 0x00F5, 0, 0) # 0x00F5 = BM_CLICK
    >
    > -Dave
    >
    >
    SectorUnknown, Sep 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gnarlodious
    Replies:
    4
    Views:
    17,579
    elizas
    May 5, 2010
  2. Replies:
    3
    Views:
    488
    Roger Upole
    Jun 15, 2006
  3. Replies:
    6
    Views:
    451
    Kent Johnson
    Jun 22, 2006
  4. bienwell
    Replies:
    5
    Views:
    5,195
    bienwell
    Aug 13, 2006
  5. Replies:
    3
    Views:
    398
Loading...

Share This Page