DEVui.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """
  2. *REALLY* Small framework for creating/manipulating/deleting gui elements in Tkinter.
  3. NOTE: keep this synced with ui.js
  4. Author: Raphael Mannadiar
  5. Date: 2014/08/21
  6. """
  7. import sys
  8. try:
  9. import Tkinter as tk
  10. except ImportError:
  11. import tkinter as tk
  12. from sccd.runtime.libs.drawing import drawing
  13. from sccd.runtime.libs.utils import utils
  14. from sccd.runtime.statecharts_core import Event
  15. class ui:
  16. simulator = None
  17. window = None
  18. __nextWindowId = 0
  19. EVENTS = utils._bunch(
  20. KEY_PRESS = '<Key>',
  21. MOUSE_CLICK = '<Button>',
  22. MOUSE_MOVE = '<Motion>',
  23. MOUSE_PRESS = '<ButtonPress>',
  24. MOUSE_RELEASE = '<ButtonRelease>',
  25. MOUSE_RIGHT_CLICK = '<Button-2>' if sys.platform == "darwin" else '<Button-3>',
  26. WINDOW_CLOSE = 'WM_DELETE_WINDOW');
  27. if sys.platform == "darwin":
  28. MOUSE_BUTTONS = utils._bunch(
  29. LEFT = 1,
  30. MIDDLE = 3,
  31. RIGHT = 2);
  32. else:
  33. MOUSE_BUTTONS = utils._bunch(
  34. LEFT = 1,
  35. MIDDLE = 2,
  36. RIGHT = 3);
  37. KEYCODES = utils._bunch(
  38. DELETE = 46);
  39. @staticmethod
  40. def append_button(_window,text):
  41. button = tk.Button(_window, text=text)
  42. button.pack(fill=tk.BOTH, expand=1)
  43. return ui.wrap_element(button)
  44. @staticmethod
  45. def append_canvas(_window,width,height,style):
  46. canvas = tk.Canvas(_window,width=width,height=height)
  47. canvas.config(**style)
  48. canvas.pack(fill=tk.BOTH, expand=1)
  49. return drawing.canvas_wrapper(canvas)
  50. @staticmethod
  51. def bind_event(source,event,controller,raise_name,port="ui"):
  52. def __handle_event(ev=None):
  53. #port="field_ui"
  54. #source = "self.State[0].field_window"
  55. if event == ui.EVENTS.KEY_PRESS :
  56. #controller.addInput(Event(raise_name, port, [ev.keycode,source]))
  57. ui.simulator.realtime_interrupt(f"{port} Event(self.{port},\"{raise_name}\",[{ev.keycode},{source}])")
  58. elif event == ui.EVENTS.MOUSE_CLICK or \
  59. event == ui.EVENTS.MOUSE_MOVE or \
  60. event == ui.EVENTS.MOUSE_PRESS or \
  61. event == ui.EVENTS.MOUSE_RELEASE or \
  62. event == ui.EVENTS.MOUSE_RIGHT_CLICK :
  63. ui.simulator.realtime_interrupt(f"{port} Event(\"{raise_name}\",self.{port},[{ev.x},{ev.y},\"{ev.num}\"])")
  64. #controller.addInput(Event(raise_name, port, [ev.x, ev.y, ev.num]))
  65. elif event == ui.EVENTS.WINDOW_CLOSE :
  66. #TODO: Quick fix, use a better version of pypdevs because this one can only send strings. Be sure to not set any spaces in the second argument otherwise it
  67. # does not register
  68. #source = "self.State[0].field_window"
  69. source = "list(self.instances)[0].field_window"
  70. ui.simulator.realtime_interrupt(f"{port} Event(\"{raise_name}\",self.{port},[{source}])")
  71. #controller.addInput(Event(raise_name, port, [source]))
  72. else:
  73. raise Exception('Unsupported event');
  74. if event == ui.EVENTS.WINDOW_CLOSE :
  75. source.protocol(event, __handle_event)
  76. elif issubclass(drawing.ui_element_wrapper,source.__class__) :
  77. source.canvas_wrapper.element.tag_bind(source.element_id, event, __handle_event)
  78. else :
  79. source.bind(event, __handle_event)
  80. @staticmethod
  81. def close_window(_window):
  82. _window.destroy()
  83. @staticmethod
  84. def log(value):
  85. print(value)
  86. @staticmethod
  87. def new_window(width,height,title=None):
  88. _window = tk.Toplevel(ui.window)
  89. if title:
  90. _window.title(title)
  91. _window.geometry(str(width)+"x"+str(height)+"+300+300")
  92. return _window
  93. @staticmethod
  94. def println(value,target):
  95. raise Exception('Not implemented yet');
  96. @staticmethod
  97. def wrap_element(element):
  98. return utils._bunch(element=element)