pygi_test.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/python3
  2. """
  3. This test program utilizes the Python bindings for GTK3 (PyGI or PyGObject).
  4. The program conists of a window with a button that closes the window when
  5. clicked. The window also shows a matplotlib plot. The plot is based on this
  6. example: http://matplotlib.org/examples/pie_and_polar_charts/polar_bar_demo.html
  7. """
  8. from gi.repository import Gtk
  9. from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
  10. from matplotlib.figure import Figure
  11. import matplotlib.cm as cm
  12. import numpy as np
  13. class MyWindow(Gtk.Window):
  14. def __init__(self):
  15. Gtk.Window.__init__(self, title="PyGI / PyGObject Example")
  16. self.connect("delete-event", Gtk.main_quit)
  17. self.set_default_size(400, 400)
  18. self.box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
  19. self.add(self.box)
  20. self.fig = Figure(figsize=(10,10), dpi=80)
  21. self.ax = self.fig.add_subplot(111, polar=True)
  22. N = 20
  23. theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
  24. radii = 10 * np.random.rand(N)
  25. width = np.pi / 4 * np.random.rand(N)
  26. bars = self.ax.bar(theta, radii, width=width, bottom=0.0)
  27. for r, bar in zip(radii, bars):
  28. bar.set_facecolor(cm.jet(r / 10.))
  29. bar.set_alpha(0.5)
  30. self.canvas = FigureCanvas(self.fig)
  31. self.box.pack_start(self.canvas, True, True, 0)
  32. self.button = Gtk.Button(label="Exit")
  33. self.button.connect("clicked", self.on_exit_clicked)
  34. self.box.pack_start(self.button, False, False, 0)
  35. def on_exit_clicked(self, widget):
  36. Gtk.main_quit()
  37. def main():
  38. win = MyWindow()
  39. win.show_all()
  40. Gtk.main()