/Demo/tkinter/guido/listtree.py

http://unladen-swallow.googlecode.com/ · Python · 37 lines · 28 code · 6 blank · 3 comment · 3 complexity · 36660f6fe400f78573b6676911c38e6c MD5 · raw file

  1. # List a remote app's widget tree (names and classes only)
  2. import sys
  3. import string
  4. from Tkinter import *
  5. def listtree(master, app):
  6. list = Listbox(master, name='list')
  7. list.pack(expand=1, fill=BOTH)
  8. listnodes(list, app, '.', 0)
  9. return list
  10. def listnodes(list, app, widget, level):
  11. klass = list.send(app, 'winfo', 'class', widget)
  12. ## i = string.rindex(widget, '.')
  13. ## list.insert(END, '%s%s (%s)' % ((level-1)*'. ', widget[i:], klass))
  14. list.insert(END, '%s (%s)' % (widget, klass))
  15. children = list.tk.splitlist(
  16. list.send(app, 'winfo', 'children', widget))
  17. for c in children:
  18. listnodes(list, app, c, level+1)
  19. def main():
  20. if not sys.argv[1:]:
  21. sys.stderr.write('Usage: listtree appname\n')
  22. sys.exit(2)
  23. app = sys.argv[1]
  24. tk = Tk()
  25. tk.minsize(1, 1)
  26. f = Frame(tk, name='f')
  27. f.pack(expand=1, fill=BOTH)
  28. list = listtree(f, app)
  29. tk.mainloop()
  30. if __name__ == '__main__':
  31. main()