diff options
Diffstat (limited to 'exercises/en/Exercise18.activity/exercise18_solution.py')
-rw-r--r-- | exercises/en/Exercise18.activity/exercise18_solution.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/exercises/en/Exercise18.activity/exercise18_solution.py b/exercises/en/Exercise18.activity/exercise18_solution.py new file mode 100644 index 0000000..da880e8 --- /dev/null +++ b/exercises/en/Exercise18.activity/exercise18_solution.py @@ -0,0 +1,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.') |