Posted to tcl by Stu at Tue Mar 05 19:09:13 GMT 2024view raw

  1. #
  2. # Tkinter Widget Wrappers
  3. #
  4. # Stuart Cassoff
  5. #
  6. # Winter 2024
  7. # Version 1.0
  8. #
  9.  
  10. import tkinter, tkinter.ttk, re
  11.  
  12. def wrap ():
  13. for items,prefix in (tkinter.__dict__.items(), ""), (tkinter.ttk.__dict__.items(), "ttk."):
  14. for item in items:
  15. if "__mro__" not in object.__dir__(item[1]) or tkinter.Widget not in item[1].__mro__: continue
  16. exec(left_align(f"""
  17. class {prefix.replace(".","_")+item[0]} (tkinter.{prefix+item[0]}):
  18. def __init__(self,*args,**kwargs):super().__init__(*args,**kwargs)
  19. def grid(self,*args,**kwargs):super().grid(*args,**kwargs);return self
  20. """), globals())
  21.  
  22. def wrap2 ():
  23. exec("\n".join([left_align(f"""
  24. class {prefix.replace(".","_")+item[0]} (tkinter.{prefix+item[0]}):
  25. def __init__(self,*args,**kwargs):super().__init__(*args,**kwargs)
  26. def grid(self,*args,**kwargs):super().grid(*args,**kwargs);return self
  27. """) for items,prefix in ((tkinter.__dict__.items(), ""), (tkinter.ttk.__dict__.items(), "ttk."))
  28. for item in items if "__mro__" in object.__dir__(item[1]) and tkinter.Widget in item[1].__mro__ ]), globals())
  29.  
  30. def left_align (blob, indent=0):
  31. res = []
  32. start = 0
  33. spaces = (" " * indent)
  34. for line in blob.splitlines():
  35. if start == 0 and (m := re.match("^ +[^ ]", line)):
  36. start = m.end() - 1
  37. res.append(spaces + line[start:])
  38. return "\n".join(res)
  39.  
  40. # EOF
  41.