advanced_calculator.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/python3
  2. import gi
  3. gi.require_version('Gtk', '3.0')
  4. from gi.repository import GLib
  5. from gi.repository import Gtk
  6. from gi.repository import GFlow
  7. from gi.repository import GtkFlow
  8. import sys
  9. class CalculatorNode(GFlow.SimpleNode):
  10. def __new__(cls, *args, **kwargs):
  11. x = GFlow.SimpleNode.new()
  12. x.__class__ = cls
  13. return x
  14. class OperationNode(CalculatorNode):
  15. def __init__(self):
  16. self.summand_a = GFlow.SimpleSink.new(float(0))
  17. self.summand_b = GFlow.SimpleSink.new(float(0))
  18. self.summand_a.set_name("operand A")
  19. self.summand_b.set_name("operand B")
  20. self.add_sink(self.summand_a)
  21. self.add_sink(self.summand_b)
  22. self.result = GFlow.SimpleSource.new(float(0))
  23. self.result.set_name("result")
  24. self.add_source(self.result)
  25. self.result.set_value(None)
  26. operations = ["+", "-", "*", "/"]
  27. self.combobox = Gtk.ComboBoxText()
  28. self.combobox.connect("changed", self.do_calculations)
  29. self.combobox.set_entry_text_column(0)
  30. for op in operations:
  31. self.combobox.append_text(op)
  32. self.summand_a.connect("changed", self.do_calculations)
  33. self.summand_b.connect("changed", self.do_calculations)
  34. self.set_name("Operation")
  35. def do_calculations(self, dock, val=None):
  36. op = self.combobox.get_active_text()
  37. val_a = self.summand_a.get_value(0)
  38. val_b = self.summand_b.get_value(0)
  39. if val_a is None or val_b is None:
  40. self.result.set_value(None)
  41. return
  42. if op == "+":
  43. self.result.set_value(val_a+val_b)
  44. elif op == "-":
  45. self.result.set_value(val_a-val_b)
  46. elif op == "*":
  47. self.result.set_value(val_a*val_b)
  48. elif op == "/":
  49. self.result.set_value(val_a/val_b)
  50. else:
  51. self.result.set_value(None)
  52. class NumberNode(CalculatorNode):
  53. def __init__(self, number=0):
  54. self.number = GFlow.SimpleSource.new(float(number))
  55. self.number.set_name("output")
  56. self.add_source(self.number)
  57. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  58. self.spinbutton = Gtk.SpinButton()
  59. self.spinbutton.set_adjustment(adjustment)
  60. self.spinbutton.set_size_request(50,20)
  61. self.spinbutton.connect("value_changed", self.do_value_changed)
  62. self.number.set_value(float(self.spinbutton.get_value()))
  63. self.set_name("NumberGenerator")
  64. def do_value_changed(self, widget=None, data=None):
  65. self.number.set_value(float(self.spinbutton.get_value()))
  66. class PrintNode(CalculatorNode):
  67. def __init__(self):
  68. self.number = GFlow.SimpleSink.new(float(0))
  69. self.number.set_name("input")
  70. self.number.connect("changed", self.do_printing)
  71. self.add_sink(self.number)
  72. self.childlabel = Gtk.Label()
  73. self.set_name("Output")
  74. def do_printing(self, dock):
  75. n = self.number.get_value(0)
  76. if n is not None:
  77. self.childlabel.set_text(str(n))
  78. else:
  79. self.childlabel.set_text("")
  80. class Calculator(object):
  81. def __init__(self):
  82. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  83. self.nv = GtkFlow.NodeView.new()
  84. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  85. create_numbernode_button = Gtk.Button("Create NumberNode")
  86. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  87. hbox.add(create_numbernode_button)
  88. create_addnode_button = Gtk.Button("Create OperationNode")
  89. create_addnode_button.connect("clicked", self.do_create_addnode)
  90. hbox.add(create_addnode_button)
  91. create_printnode_button = Gtk.Button("Create PrintNode")
  92. create_printnode_button.connect("clicked", self.do_create_printnode)
  93. hbox.add(create_printnode_button)
  94. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  95. vbox.pack_start(hbox, False, False, 0)
  96. vbox.pack_start(self.nv, True, True, 0)
  97. w.add(vbox)
  98. w.add(self.nv)
  99. w.show_all()
  100. w.connect("destroy", self.do_quit)
  101. Gtk.main()
  102. def do_create_addnode(self, widget=None, data=None):
  103. n = OperationNode()
  104. self.nv.add_with_child(n, n.combobox)
  105. self.nv.set_node_renderer(n, GtkFlow.NodeRendererType.DOCKLINE)
  106. def do_create_numbernode(self, widget=None, data=None):
  107. n = NumberNode()
  108. self.nv.add_with_child(n, n.spinbutton)
  109. self.nv.set_node_renderer(n, GtkFlow.NodeRendererType.DOCKLINE)
  110. def do_create_printnode(self, widget=None, data=None):
  111. n = PrintNode()
  112. self.nv.add_with_child(n, n.childlabel)
  113. def do_quit(self, widget=None, data=None):
  114. Gtk.main_quit()
  115. sys.exit(0)
  116. if __name__ == "__main__":
  117. Calculator()