diff options
Diffstat (limited to 'exercises/en/Exercise18.activity/exercise18.py')
-rw-r--r-- | exercises/en/Exercise18.activity/exercise18.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/exercises/en/Exercise18.activity/exercise18.py b/exercises/en/Exercise18.activity/exercise18.py new file mode 100644 index 0000000..753eb4a --- /dev/null +++ b/exercises/en/Exercise18.activity/exercise18.py @@ -0,0 +1,99 @@ +#!/usr/bin/python +# 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 SimpleWindow(Gtk.Window): + """A simple window which displays a label and a button.""" + def __init__(self): + super(SimpleWindow, self).__init__(title='Simple Window') + + # Set a default size for the window. + self.set_default_size(200, 200) + + # Set up a close handler for the window. Do not modify this. + self.connect('destroy', self.__window_destroy_cb) + + # 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.add(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.') + + def __window_destroy_cb(self, window): + """Callback to quit the program when the window is closed. + + Do not modify this.""" + Gtk.main_quit() |