ui.py 2.7 KB

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