Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/exercises/en/17_button-and-label_solution.py
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/en/17_button-and-label_solution.py')
-rw-r--r--exercises/en/17_button-and-label_solution.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/exercises/en/17_button-and-label_solution.py b/exercises/en/17_button-and-label_solution.py
new file mode 100644
index 0000000..30d644b
--- /dev/null
+++ b/exercises/en/17_button-and-label_solution.py
@@ -0,0 +1,77 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""This program should display a GTK+ window containing a button and label.
+
+You must add two widgets to the box in the window: a Gtk.Button and a
+Gtk.Label.
+
+The Gtk.Button must show the text 'Change Label'. The Gtk.Label must show the
+text 'Initial label.'.
+
+After you have added the two widgets, you must connect a callback to the
+button's "clicked" signal. In this callback, you must change the Gtk.Label's
+text to 'Final label.'. This means that when the program is run and the button
+is clicked, the label should change its text.
+
+You can find documentation for the Gtk.Button "clicked" signal here:
+ https://developer.gnome.org/gtk3/stable/GtkButton.html#GtkButton-clicked
+
+Don't forget to document any new methods you add.
+
+Hint: Don't forget to call the show() method on the button and label to make
+them visible after adding them to the box.
+"""
+
+from gi.repository import Gtk
+
+
+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()
+
+
+# Run the program. Do not modify this.
+SimpleWindow().show()
+Gtk.main()