Posted to tcl by Stu at Tue Mar 05 19:09:13 GMT 2024view raw
- #
- # Tkinter Widget Wrappers
- #
- # Stuart Cassoff
- #
- # Winter 2024
- # Version 1.0
- #
- import tkinter, tkinter.ttk, re
- def wrap ():
- for items,prefix in (tkinter.__dict__.items(), ""), (tkinter.ttk.__dict__.items(), "ttk."):
- for item in items:
- if "__mro__" not in object.__dir__(item[1]) or tkinter.Widget not in item[1].__mro__: continue
- exec(left_align(f"""
- class {prefix.replace(".","_")+item[0]} (tkinter.{prefix+item[0]}):
- def __init__(self,*args,**kwargs):super().__init__(*args,**kwargs)
- def grid(self,*args,**kwargs):super().grid(*args,**kwargs);return self
- """), globals())
- def wrap2 ():
- exec("\n".join([left_align(f"""
- class {prefix.replace(".","_")+item[0]} (tkinter.{prefix+item[0]}):
- def __init__(self,*args,**kwargs):super().__init__(*args,**kwargs)
- def grid(self,*args,**kwargs):super().grid(*args,**kwargs);return self
- """) for items,prefix in ((tkinter.__dict__.items(), ""), (tkinter.ttk.__dict__.items(), "ttk."))
- for item in items if "__mro__" in object.__dir__(item[1]) and tkinter.Widget in item[1].__mro__ ]), globals())
- def left_align (blob, indent=0):
- res = []
- start = 0
- spaces = (" " * indent)
- for line in blob.splitlines():
- if start == 0 and (m := re.match("^ +[^ ]", line)):
- start = m.end() - 1
- res.append(spaces + line[start:])
- return "\n".join(res)
- # EOF