Here it is in 2022 and this post just helped me, so I thought I would share what I wrote to accomplish this. I am running v8.1.18 at this time but I don’t think that matters.
I put this code on a button on a window I want to loop through and run it.
Notes:
- You’ll see some "if 1 == 1:"s in there. That’s so you can drop in a try…except…if you wish and the indentation level stays correct.
- In the function you’ll see a couple of tasks I wanted to perform…I checked if a component had a tootip defined and I set all components to visible
- The depth at which each component is found is tracked, so you could filter your actions based on that.
- There is something unusual going on with this script though. Even on a window with relatively few components, this script will only print to the console once. The second time I click the button, nothing gets printed to the console, but the window does not appear to be locked up. If anyone has any insight into that, that would be greatly appreciated.
If anyone wishes to comment on my coding style, feel free. I deliberately made this a bit more “verbose” than befits Python because eventually someone a lot less experienced than I am will be looking at this code.
def lvlTasks(lv,cn,comp,llen,cListAll):
print '\r\n'
print '\t' * (lv-1) + 'Level: %s' % (lv)
print '\t' * (lv-1) + 'Number of components at this level: %s' % (llen)
print '\t' * (lv-1) + 'Level %s - Component %s of %s Component Name %s: Component type: %s:' % (lv,cn,llen,comp.name,type(comp))
if comp.name is None:
nme = 'None'
else:
nme = str(comp.name)
cListAll.append(nme + ' at Level %s' % (lv))
try:
print '\t' * (lv-1) + 'Mouseover Text is: %s' % (comp.toolTipText)
except:
pass
try:
comp.visible = 1
except:
pass
return cListAll
win = system.gui.getParentWindow(event)
cmpList1 = win.getComponents()
lvl = 0
idx = 0
jdx = 0
kdx = 0
ldx = 0
mdx = 0
ndx = 0
odx = 0
pdx = 0
qdx = 0
lv1CmpCnt = 0
lv2CmpCnt = 0
lv3CmpCnt = 0
lv4CmpCnt = 0
lv5CmpCnt = 0
lv6CmpCnt = 0
lv7CmpCnt = 0
lv8CmpCnt = 0
lv9CmpCnt = 0
cListAll = []
for cmpLv1 in cmpList1:
lvl += 1
lv1CmpCnt += 1
idx += 1
print "\r\n\r\nLevel 1\r\nNumber of components at this level: %s" % (len(cmpList1))
print "Level 1 - Component %s of %s Component Name %s: Component type: %s:" % (idx,len(cmpList1),cmpLv1.name,type(cmpLv1))
# try:
if 1 == 1:
lvl += 1
cmpList2 = cmpLv1.getComponents()
jdx = 0
for cmpLv2 in cmpList2:
jdx += 1
lv2CmpCnt += 1
cListAll = lvlTasks(lvl,jdx,cmpLv2,len(cmpList2),cListAll)
if 1 == 1:
lvl += 1
cmpList3 = cmpLv2.getComponents()
kdx = 0
for cmpLv3 in cmpList3:
kdx += 1
lv3CmpCnt += 1
cListAll = lvlTasks(lvl,kdx,cmpLv3,len(cmpList3),cListAll)
# try:
if 1 == 1:
lvl += 1
cmpList4 = cmpLv3.getComponents()
ldx = 0
for cmpLv4 in cmpList4:
ldx += 1
lv4CmpCnt += 1
cListAll = lvlTasks(lvl,ldx,cmpLv4,len(cmpList4),cListAll)
if 1==1:
# try:
lvl += 1
cmpList5 = cmpLv4.getComponents()
mdx = 0
for cmpLv5 in cmpList5:
mdx += 1
lv5CmpCnt += 1
cListAll = lvlTasks(lvl,mdx,cmpLv5,len(cmpList5),cListAll)
# try:
if 1==1:
lvl += 1
cmpList6 = cmpLv5.getComponents()
ndx = 0
for cmpLv6 in cmpList6:
ndx += 1
lv6CmpCnt += 1
cListAll = lvlTasks(lvl,ndx,cmpLv6,len(cmpList6),cListAll)
if 1 == 1:
lvl += 1
cmpList7 = cmpLv6.getComponents()
odx = 0
for cmpLv7 in cmpList7:
odx += 1
lv7CmpCnt += 1
cListAll = lvlTasks(lvl,odx,cmpLv7,len(cmpList7),cListAll)
if 1 == 1:
lvl += 1
cmpList8 = cmpLv7.getComponents()
pdx = 0
for cmpLv8 in cmpList8:
pdx += 1
lv8CmpCnt += 1
cListAll = lvlTasks(lvl,pdx,cmpLv8,len(cmpList8),cListAll)
if 1 == 1:
lvl += 1
cmpList9 = cmpLv8.getComponents()
qdx = 0
for cmpLv9 in cmpList9:
qdx += 1
lv9CmpCnt += 1
cListAll = lvlTasks(lvl,odx,cmpLv9,len(cmpList9),cListAll)
lvl -= 1
lvl -= 1
lvl -= 1
lvl -= 1
# except:
# pass
lvl -= 1
# except:
# pass
lvl -= 1
# except:
# pass
lvl -= 1
# except:
# pass
lvl -= 1
# except:
# pass
lvl -= 1
print 'Components at Level 1: %s\r\nComponents at Level 2: %s\r\nComponents at Level 3: %s\r\nComponents at Level 4: %s' \
'\r\nComponents at Level 5: %s: \r\nComponents at Level 6: %s\r\nComponents at Level 7: %s: \r\nComponents at Level 8: %s' \
'\r\nComponents at Level 9: %s: \r\n' % (lv1CmpCnt,lv2CmpCnt,lv3CmpCnt,lv4CmpCnt,lv5CmpCnt,lv6CmpCnt,lv7CmpCnt,lv8CmpCnt,lv9CmpCnt)
for c in cListAll:
print c