Home > front end >  how to read window dialog fields values using python?
how to read window dialog fields values using python?

Time:12-10

I try to read values from fields of a window dialog.

In the dialog configuration I have this:

#define IDC_SYMBOL                      8011
#define IDC_SHARES                      8012

dialog snapshot picture

this is my simple code tried:

import win32gui,win32con

wT="my_dialog"
dlg=win32gui.FindWindow(None,wT)

d1=win32gui.GetDlgItem(dlg, 8011)
d2=win32gui.GetDlgItem(dlg, 8012)

print('d1: ', d1)
print('d2: ', d2)


def dumpWindow(hwnd, wantedText=None, wantedClass=None):
    windows = []
    hwndChild = None
    while True:
        hwndChild = win32gui.FindWindowEx(hwnd, hwndChild, wantedClass, wantedText)
        if hwndChild:
            textName = win32gui.GetWindowText(hwndChild)
            className = win32gui.GetClassName(hwndChild)
            windows.append((hwndChild, textName, className))
        else:
            return windows 


windows = dumpWindow(dlg, wantedText='Symbol')

print(windows)

And this is what I get when running it:

d1:  1051340
d2:  920324
[(9112016, 'Symbol', 'Static')]

How do I read the actual values in those window dialog fields?

I also tried to get the dialog item text:

dText=win32gui.GetDlgItemText(dlg, 8011)
print('dText: ', dText)

and result is empty while the expected result is 'AAPL' for example, or whatever is there.

dText:

CodePudding user response:

Why you don't use GetDlgItemText.

Normally, this function will return the text of a dialog control

CodePudding user response:

Solution:

from sys import exit
import win32gui,win32con

wT="my_dialog"
dlg=win32gui.FindWindow(None,wT)

d1=win32gui.GetDlgItem(dlg, 8011) #symbol

def getEditText(hwnd):
     buffer = win32gui.PyMakeBuffer(255)
     bufLen = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 255, buffer)
     win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, 255, buffer)
     text = buffer[0:bufLen*2] 
     return text 

print ('Symbol: ', str(getEditText(d1),'utf-16'))

this script prints the selected symbol:

Symbol: ZVZZT
  • Related