FISH-Tech

Strive

Oct 22nd, 2020 (edited)
957
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. try:
  2.     import tkinter as tk
  3. except:
  4.     print("Strive: module 'tkinter' is required")
  5.  
  6. import json
  7.  
  8. windows = {}
  9.  
  10. events_windows = []
  11.  
  12. def add_window(filename):
  13.     content = open(filename,"r").read()
  14.     parsed = json.loads(content)
  15.     windows[parsed["id"]] = filename
  16.  
  17. def add_event_to_window(window,event,function):
  18.     events_windows.append({
  19.         "window": window,
  20.         "event": event,
  21.         "function": function
  22.     })
  23.  
  24. def show_window(id):
  25.     filename = windows[id]
  26.     content = open(filename,"r").read()
  27.     parsed = json.loads(content)
  28.     widgets = parsed["widgets"]
  29.  
  30.     event_onWidgetsLoaded = None
  31.     event_onWindowLoaded = None
  32.     event_onWindowConfigSet = None
  33.  
  34.     for event in events_windows:
  35.         if event["window"] == id:
  36.             if event["event"] == "onWidgetsLoaded":
  37.                 event_onWidgetsLoaded = event["function"]
  38.             elif event["event"] == "onWindowLoaded":
  39.                 event_onWindowLoaded = event["function"]
  40.             elif event["event"] == "onWindowConfigSet":
  41.                 event_onWindowConfigSet = event["function"]
  42.    
  43.     exec(f"global {id};{id} = tk.Tk()")
  44.     if "title" in parsed:
  45.         exec(f"{id}.title(\"{parsed['title']}\")")
  46.     else:
  47.         exec(f"{id}.title(\"Strive Window\")")
  48.     if "width" in parsed and "height" in parsed:
  49.         exec(f"{id}.geometry(\"{str(parsed['width'])}x{str(parsed['height'])}\")")
  50.     else:
  51.         exec(f"{id}.geometry(\"500x500\")")
  52.  
  53.     if event_onWindowLoaded:
  54.         event_onWindowLoaded()
  55.  
  56.     for widget in widgets:
  57.         exec(f"global {widget['id']};{widget['id']} = tk.{widget['type']}({id})")
  58.         for setting in widget:
  59.             if setting not in ["type","id","position"]:
  60.                 if widget[setting] not in ["True","False"] and not isinstance(widget[setting],int):
  61.                     exec(f"{widget['id']}[\"{setting}\"] = \"{widget[setting]}\"")
  62.                 else:
  63.                     exec(f"{widget['id']}[\"{setting}\"] = {widget[setting]}")
  64.             elif setting == "position":
  65.                 pos = widget["position"]
  66.                 pos_func = f"{widget['id']}.{pos['mode']}("
  67.                 for pos_setting in pos:
  68.                     if pos_setting != "mode":
  69.                         if isinstance(pos[pos_setting],str):
  70.                             pos_func += f"{pos_setting}=\"{pos[pos_setting]}\""
  71.                         else:
  72.                             pos_func += f"{pos_setting}={pos[pos_setting]}"
  73.  
  74.                         if pos_setting != list(pos)[-1]:
  75.                             pos_func += ","
  76.                 pos_func += ")"
  77.                 exec(pos_func)
  78.                    
  79.     if event_onWidgetsLoaded:
  80.         event_onWidgetsLoaded()
  81.  
  82.     config = parsed["config"]
  83.     config_func = f"{id}.config("
  84.     for configuration in config:
  85.         for setting in configuration:
  86.             if setting != "attribute":
  87.                 if isinstance(configuration[setting],str):
  88.                     config_func += f"{configuration['attribute']}=\"{configuration['value']}\""
  89.                 else:
  90.                     config_func += f"{configuration['attribute']}={configuration['value']}"
  91.                
  92.                 if setting != list(configuration)[-1]:
  93.                     config_func += ","
  94.     config_func += ")"
  95.     exec(config_func)
  96.  
  97.     if event_onWindowConfigSet:
  98.         event_onWindowConfigSet()
  99.  
  100.     exec(f"{id}.mainloop()")
  101.  
  102. def hide_window(window):
  103.     exec(f"{window}.destroy()")
RAW Paste Data