1from gi.repository import Gtk
2from gi.repository import cairo
3import sys
4import math
5
6
7class MyWindow(Gtk.ApplicationWindow):
8
9    def __init__(self, app):
10        Gtk.Window.__init__(self, title="Choose an angle", application=app)
11        self.set_default_size(400, 400)
12        self.set_border_width(10)
13
14        # a default angle
15        self.angle = 360
16
17        grid = Gtk.Grid()
18
19        # a spinbutton that takes the value of an angle
20        ad = Gtk.Adjustment(360, 0, 360, 1, 0, 0)
21        self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
22        self.spin.connect("value-changed", self.get_angle)
23
24        # a drawing area for drawing whatever we want
25        self.darea = Gtk.DrawingArea()
26        # that we describe in the method draw(), connected to the signal "draw"
27        self.darea.connect("draw", self.draw)
28        # we have to request a minimum size of the drawing area, or it will
29        # disappear
30        self.darea.set_size_request(300, 300)
31
32        grid.attach(self.spin, 0, 0, 1, 1)
33        grid.attach(self.darea, 0, 1, 1, 1)
34
35        self.add(grid)
36
37    # whenever we get a new angle in the spinbutton
38    def get_angle(self, event):
39        self.angle = self.spin.get_value_as_int()
40        # redraw what is in the drawing area
41        self.darea.queue_draw()
42
43    def draw(self, darea, cr):
44        # a 10-pixels-wide line
45        cr.set_line_width(10)
46        # red
47        cr.set_source_rgba(0.5, 0.0, 0.0, 1.0)
48
49        # get the width and height of the drawing area
50        w = self.darea.get_allocated_width()
51        h = self.darea.get_allocated_height()
52
53        # move to the center of the drawing area
54        # (translate from the top left corner to w/2, h/2)
55        cr.translate(w / 2, h / 2)
56        # draw a line to (55, 0)
57        cr.line_to(55, 0)
58        # and get back to (0, 0)
59        cr.line_to(0, 0)
60        # draw an arc centered in the origin, 50 pixels wide, from the angle 0
61        # (in radians) to the angle given by the spinbutton (in degrees)
62        cr.arc(0, 0, 50, 0, self.angle * (math.pi / 180))
63        # draw a line back to the origin
64        cr.line_to(0, 0)
65        # drawing the path, and keeping the path for future use
66        cr.stroke_preserve()
67
68        # set a colour
69        cr.set_source_rgba(0.0, 0.5, 0.5, 1.0)
70        # and use it to fill the path (that we had kept)
71        cr.fill()
72
73
74class MyApplication(Gtk.Application):
75
76    def __init__(self):
77        Gtk.Application.__init__(self)
78
79    def do_activate(self):
80        win = MyWindow(self)
81        win.show_all()
82
83    def do_startup(self):
84        Gtk.Application.do_startup(self)
85
86app = MyApplication()
87exit_status = app.run(sys.argv)
88sys.exit(exit_status)
89