multisink.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 NumberNode(CalculatorNode):
  15. def __init__(self, number=0):
  16. self.number = GFlow.SimpleSource.new(float(number))
  17. self.number.set_name("output")
  18. self.add_source(self.number)
  19. adjustment = Gtk.Adjustment.new(0, 0, 100, 1, 10, 0)
  20. self.spinbutton = Gtk.SpinButton()
  21. self.spinbutton.set_adjustment(adjustment)
  22. self.spinbutton.set_size_request(50,20)
  23. self.spinbutton.connect("value_changed", self.do_value_changed)
  24. self.number.set_value(float(self.spinbutton.get_value()))
  25. self.set_name("NumberGenerator")
  26. def do_value_changed(self, widget=None, data=None):
  27. self.number.set_value(float(self.spinbutton.get_value()))
  28. class PrintNode(CalculatorNode):
  29. def __init__(self):
  30. self.number = GFlow.SimpleSink.new(float(0))
  31. self.number.set_name("input")
  32. self.number.connect("changed", self.do_printing)
  33. self.number.set_max_sources(10)
  34. self.add_sink(self.number)
  35. self.childlabel = Gtk.Label()
  36. self.set_name("Output")
  37. def do_printing(self, dock):
  38. report = ""
  39. for i in range(len(self.number.get_sources())):
  40. n = self.number.get_value(i)
  41. if n is not None:
  42. report += "Dock #{} contains value: {}\n".format(i, n)
  43. self.childlabel.set_text(report)
  44. class Calculator(object):
  45. def __init__(self):
  46. w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
  47. self.nv = GtkFlow.NodeView.new()
  48. hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  49. create_numbernode_button = Gtk.Button("Create NumberNode")
  50. create_numbernode_button.connect("clicked", self.do_create_numbernode)
  51. hbox.add(create_numbernode_button)
  52. create_printnode_button = Gtk.Button("Create PrintNode")
  53. create_printnode_button.connect("clicked", self.do_create_printnode)
  54. hbox.add(create_printnode_button)
  55. vbox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  56. vbox.pack_start(hbox, False, False, 0)
  57. vbox.pack_start(self.nv, True, True, 0)
  58. w.add(vbox)
  59. w.add(self.nv)
  60. w.show_all()
  61. w.connect("destroy", self.do_quit)
  62. Gtk.main()
  63. def do_create_numbernode(self, widget=None, data=None):
  64. n = NumberNode()
  65. self.nv.add_with_child(n, n.spinbutton)
  66. def do_create_printnode(self, widget=None, data=None):
  67. n = PrintNode()
  68. self.nv.add_with_child(n, n.childlabel)
  69. def do_quit(self, widget=None, data=None):
  70. Gtk.main_quit()
  71. sys.exit(0)
  72. if __name__ == "__main__":
  73. Calculator()