ui.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. window = None
  17. __nextWindowId = 0
  18. EVENTS = utils._bunch(
  19. KEY_PRESS = '<Key>',
  20. MOUSE_CLICK = '<Button>',
  21. MOUSE_MOVE = '<Motion>',
  22. MOUSE_PRESS = '<ButtonPress>',
  23. MOUSE_RELEASE = '<ButtonRelease>',
  24. MOUSE_RIGHT_CLICK = '<Button-2>' if sys.platform == "darwin" else '<Button-3>',
  25. WINDOW_CLOSE = 'WM_DELETE_WINDOW');
  26. if sys.platform == "darwin":
  27. MOUSE_BUTTONS = utils._bunch(
  28. LEFT = 1,
  29. MIDDLE = 3,
  30. RIGHT = 2);
  31. else:
  32. MOUSE_BUTTONS = utils._bunch(
  33. LEFT = 1,
  34. MIDDLE = 2,
  35. RIGHT = 3);
  36. KEYCODES = utils._bunch(
  37. DELETE = 46);
  38. @staticmethod
  39. def append_button(_window,text):
  40. button = tk.Button(_window, text=text)
  41. button.pack(fill=tk.BOTH, expand=1)
  42. return ui.wrap_element(button)
  43. @staticmethod
  44. def append_canvas(_window,width,height,style):
  45. canvas = tk.Canvas(_window,width=width,height=height)
  46. canvas.config(**style)
  47. canvas.pack(fill=tk.BOTH, expand=1)
  48. return drawing.canvas_wrapper(canvas)
  49. @staticmethod
  50. def bind_event(source,event,controller,raise_name,port="ui"):
  51. def __handle_event(ev=None):
  52. if event == ui.EVENTS.KEY_PRESS :
  53. controller.addInput(Event(raise_name, port, [ev.keycode,source]))
  54. elif event == ui.EVENTS.MOUSE_CLICK or \
  55. event == ui.EVENTS.MOUSE_MOVE or \
  56. event == ui.EVENTS.MOUSE_PRESS or \
  57. event == ui.EVENTS.MOUSE_RELEASE or \
  58. event == ui.EVENTS.MOUSE_RIGHT_CLICK :
  59. controller.addInput(Event(raise_name, port, [ev.x, ev.y, ev.num]))
  60. elif event == ui.EVENTS.WINDOW_CLOSE :
  61. controller.addInput(Event(raise_name, port, [source]))
  62. else :
  63. raise Exception('Unsupported event');
  64. if event == ui.EVENTS.WINDOW_CLOSE :
  65. source.protocol(event, __handle_event)
  66. elif issubclass(drawing.ui_element_wrapper,source.__class__) :
  67. source.canvas_wrapper.element.tag_bind(source.element_id, event, __handle_event)
  68. else :
  69. source.bind(event, __handle_event)
  70. @staticmethod
  71. def close_window(_window):
  72. _window.destroy()
  73. @staticmethod
  74. def log(value):
  75. print(value)
  76. @staticmethod
  77. def new_window(width,height,title=None):
  78. _window = tk.Toplevel(ui.window)
  79. if title:
  80. _window.title(title)
  81. _window.geometry(str(width)+"x"+str(height)+"+300+300")
  82. return _window
  83. @staticmethod
  84. def println(value,target):
  85. raise Exception('Not implemented yet');
  86. @staticmethod
  87. def wrap_element(element):
  88. return utils._bunch(element=element)