diff options
Diffstat (limited to 'exercises/en/17_button-and-label.py')
-rw-r--r-- | exercises/en/17_button-and-label.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/exercises/en/17_button-and-label.py b/exercises/en/17_button-and-label.py new file mode 100644 index 0000000..2eee908 --- /dev/null +++ b/exercises/en/17_button-and-label.py @@ -0,0 +1,64 @@ + +#!/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) + + # You must add code here to create and set up the button and label. + # Don't forget to make them visible and add them to the box. + + # You must add a signal callback here to handle the button's "clicked" + # signal and change the label's text. + + 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() |