Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a window title and scan it every 100ms use python?

with python, I want to get a window title,a stock software window.

the window's title will change when I browse another stock ,now I want to scan it every 100ms and return the new title,but the front text in the window title is the same text.

I can print the title in cmd,but I don't know how to scan it every 100ms and return

I use this code:

from win32gui import *
import re

titles = set() 
titlekey = ''

def foo(hwnd,nouse):
    if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
        titles.add(GetWindowText(hwnd))

EnumWindows(foo, 0) 
lt = [t for t in titles if t] 
lt.sort() 
for t in lt:
    if re.match(titlekey,t):
        print t

How to scan every 100ms and return the new title when it change?

like image 248
sword Avatar asked Apr 18 '12 11:04

sword


1 Answers

Put it in a loop:

import win32gui
tempWindowName=win32gui.GetWindowText (win32gui.GetForegroundWindow())
import time
while True:
    if (tempWindowName==win32gui.GetWindowText (win32gui.GetForegroundWindow()))
        pass
    else
        tempWindowName=win32gui.GetWindowText (win32gui.GetForegroundWindow())
        #do what you want
    time.sleep(0.1)
like image 187
user850498 Avatar answered Oct 05 '22 20:10

user850498