multisink.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. import gi
  3. gi.require_version('Gtk', '3.0')
  4. gi.require_version('GtkFlow', '0.10')
  5. gi.require_version('GFlow', '0.10')
  6. from gi.repository import GLib
  7. from gi.repository import Gtk
  8. from gi.repository import GFlow
  9. from gi.repository import GtkFlow
  10. import sys
  11. class CalculatorNode(GFlow.SimpleNode):
  12. def __new__(cls, *args, **kwargs):
  13. x = GFlow.SimpleNode.new()
  14. x.__class__ = cls
  15. return x
  16. class NumberNode(CalculatorNode):
  17. def __init__(self, number=0):
  18. self.number = GFlow.SimpleSource.new(float(number))
  19. self.number.set_name("output")
  20. self.add_source(self.number)
  21. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  22. self.spinbutton = Gtk.SpinButton()
  23. self.spinbutton.set_adjustment(adjustment)
  24. self.spinbutton.set_size_request(50,20)
  25. self.spinbutton.connect("value_changed", self.do_value_changed)
  26. self.number.set_value(float(self.spinbutton.get_value()))
  27. self.set_name("NumberGenerator")
  28. def do_value_changed(self, widget=None, data=None):
  29. self.number.set_value(float(self.spinbutton.get_value()))
  30. class PrintNode(CalculatorNode):
  31. def __init__(self):
  32. self.number = GFlow.SimpleSink.new(float(0))
  33. self.number.set_name("input")
  34. self.number.connect("changed", self.do_printing)
  35. self.number.set_max_sources(10)
  36. self.add_sink(self.number)
  37. self.childlabel = Gtk.Label()
  38. self.set_name("Output")
  39. def do_printing(self, dock):
  40. report = ""
  41. for i in range(len(self.number.get_sources())):
  42. n = self.number.get_value(i)
  43. if n is not None:
  44. report += "Dock #{} contains value: {}\n".format(i, n)
  45. self.childlabel.set_text(report)
  46. class Calculator(object):
  47. def __init__(self):
  48. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  49. self.nv = GtkFlow.NodeView.new()
  50. self.nv.set_placeholder("Please click the buttons above to spawn nodes.")
  51. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  52. create_numbernode_button = Gtk.Button("Create NumberNode")
  53. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  54. hbox.add(create_numbernode_button)
  55. create_printnode_button = Gtk.Button("Create PrintNode")
  56. create_printnode_button.connect("clicked", self.do_create_printnode)
  57. hbox.add(create_printnode_button)
  58. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  59. vbox.pack_start(hbox, False, False, 0)
  60. vbox.pack_start(self.nv, True, True, 0)
  61. w.add(vbox)
  62. w.add(self.nv)
  63. w.show_all()
  64. w.connect("destroy", self.do_quit)
  65. Gtk.main()
  66. def do_create_numbernode(self, widget=None, data=None):
  67. n = NumberNode()
  68. self.nv.add_with_child(n, n.spinbutton)
  69. def do_create_printnode(self, widget=None, data=None):
  70. n = PrintNode()
  71. self.nv.add_with_child(n, n.childlabel)
  72. def do_quit(self, widget=None, data=None):
  73. Gtk.main_quit()
  74. sys.exit(0)
  75. if __name__ == "__main__":
  76. Calculator()