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 use the code below to activate the command promt:

hwn = win32gui.FindWindow(None, "Prompt dei comandi - python demo.py")
win32gui.SetForegroundWindow(hwn)

Prompt dei comandi is the italian word of command promt :) and demo.py is the name of the python file. It works perfectly with window xp but when i try the same code with win7 it doesn't work anymore. I also check the list of the visible windows with this code:

import win32gui
def window_enum_handler(hwnd, resultList):
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
        resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def get_app_list(handles=[]):
    mlst=[]
    win32gui.EnumWindows(window_enum_handler, handles)
    for handle in handles:
        mlst.append(handle)
    return mlst
appwindows = get_app_list()
return appwindows
for i in appwindows:
   print i

In that list i can see: Prompt dei comandi - python demo.py What does it change from win xp to win7??

Thanks

There's a couple of things that could be. But I think it's about ANSI and UNICODE. Try using:

hwn = win32gui.FindWindow(None, u"Prompt dei comandi - python demo.py")

Or maybe using ctypes:

from ctypes import windll
FindWindowW = windll.user32.FindWindowW
FindWindowA = windll.user32.FindWindowA
print FindWindowW(0, u"Prompt dei comandi - python demo.py")
print FindWindowA(0, "Prompt dei comandi - python demo.py")

Hope it helps.

It turned out that the only different was just on one space! i will explain my self: this works for win xp:

hwn = win32gui.FindWindow(None, "Prompt dei comandi - python demo.py")

this works for win7:

hwn = win32gui.FindWindow(None, "Prompt dei comandi - python  demo.py")

it sounds crazy but it does.

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.