相关文章推荐
体贴的路灯  ·  如何从SQL ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to ge the handle for "Yes" button in a dialog, so I can send the message to click it.

I get the dialog and then I try to find the button, but I always get 0 back.

import win32gui
hwnd = win32gui.FindWindow("#32770", "Programs and Features")
# got back the correct handle to the dialog
win32gui.SetForegroundWindow(hwnd)
btnhdl = win32gui.FindWindowEx(hwnd, 0, "Button", "&Yes")
# returns 0

The button is there and the class and title seem to be ok. I verified it by this:

def printClasses(childHwnd, lparam):
    if win32gui.GetWindowText(childHwnd) == "&Yes":
        print win32gui.GetClassName(childHwnd), win32gui.GetWindowText(childHwnd)
    return 1
win32gui.EnumChildWindows(hwnd, printClasses, None)
# output: Button &Yes

Looks like everything should be fine, but why it doesn't return the handle with FindWindowEx?

Thanks

Maybe the button is a child of a child, ie a grandchild? IIRC EnumChildWindow enumerates recursively while FindWindowEx does not. – rodrigo May 10, 2012 at 19:19 @rodrigo Yes, that is correct. It is actually two levels down. From the question EnumChildWindows or FindWindowEx I got an impression that they work similar way. You should probably post it as an answer, so I can accept it. Thanks – Alex Okrushko May 10, 2012 at 20:03

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.