Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/exercises/en/Exercise18.activity/exercise18_solution.py
blob: da880e84920f5680c2fd0561a18fbe07e8f51989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# coding=utf-8

"""Convert a simple GTK+ program to be a Sugar activity.

You must convert the simple GTK+ program from exercise 17 to be a Sugar
activity.

All the other files for the activity (activity.info, setup.py, etc.) have been
provided already; you must simply modify the code in this file to derive the
class from sugar3.activity.activity.Activity instead of from Gtk.Window, and
make a few other changes.

 1. Remove the “#!/usr/bin/python” line from the top of the file.
    The first line of the file should now be “# coding=utf-8”.
 2. Rename the class to “Exercise18Activity”.
 3. Add a ‘handle’ parameter to __init__ and propagate it to the parent
    constructor in the super() call.
 4. Remove the window title from the constructor super() call.
 5. Remove the call to set_default_size() and the window destroy callback.
 6. Add an activity toolbar (ToolbarBox), and add an activity button
    (ActivityToolbarButton) and a stop button (StopButton) to it.
 7. Change the self.add() call to self.set_canvas().
 8. In a terminal, change to this directory and run ‘./setup.py dev’ to add the
    activity to Sugar on your computer.
 9. In a terminal, run ‘sugar-emulator’ and test the activity.

Note that the necessary ‘import’ statements have already been added to the
file.

Don't forget to document any new methods you add.

Hint: The code necessary for step 6 is:
        # Create the standard activity toolbox.
        toolbar_box = ToolbarBox()
        self.set_toolbar_box(toolbar_box)
        toolbar_box.show()

        main_toolbar = toolbar_box.toolbar

        activity_toolbar_button = widgets.ActivityToolbarButton(self)
        main_toolbar.insert(activity_toolbar_button, 0)
        activity_toolbar_button.show()

        stop_button = widgets.StopButton(self)
        stop_button.show()
        main_toolbar.insert(stop_button, -1)
"""

from gi.repository import Gtk
from sugar3.activity import activity, widgets
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbutton import ToolButton


class Exercise18Activity(activity.Activity):
    """A simple window which displays a label and a button."""
    def __init__(self, handle):
        super(Exercise18Activity, self).__init__(handle)

        # Create the standard activity toolbox.
        toolbar_box = ToolbarBox()
        self.set_toolbar_box(toolbar_box)
        toolbar_box.show()

        main_toolbar = toolbar_box.toolbar

        activity_toolbar_button = widgets.ActivityToolbarButton(self)
        main_toolbar.insert(activity_toolbar_button, 0)
        activity_toolbar_button.show()

        stop_button = widgets.StopButton(self)
        stop_button.show()
        main_toolbar.insert(stop_button, -1)

        # Add a box to the window.
        box = Gtk.Box()
        box.set_orientation(Gtk.Orientation.VERTICAL)
        box.set_spacing(8)
        box.set_border_width(8)
        box.show()
        self.set_canvas(box)

        # Create and set up the label and button.
        label = Gtk.Label('Initial label.')
        label.show()
        box.pack_start(label, True, True, 0)

        button = Gtk.Button('Change Label')
        button.show()
        box.pack_start(button, False, True, 0)

        # Connect the button callback.
        button.connect('clicked', self.__button_clicked_cb)

        # Store the label as an object variable so it can be accessed from the
        # button callback.
        self._label = label

    def __button_clicked_cb(self, button):
        """Callback to change the label text when the button is clicked."""
        self._label.set_text('Final label.')