|
#!/usr/local/cpython-3.6/bin/python3 |
|
|
|
# pylint: disable=wrong-import-position |
|
|
|
"""Give a toggle button, see what it looks like.""" |
|
|
|
import sys |
|
|
|
import gi |
|
gi.require_version('Gtk', '3.0') |
|
from gi.repository import Gtk, Gdk # noqa |
|
|
|
CSS = b''' |
|
#MyToggleButton:checked { |
|
background: red; |
|
} |
|
''' |
|
|
|
|
|
def make_used(thing): |
|
"""Persuade pyflakes that we don't want to worry about 'thing' not being used.""" |
|
assert True or thing |
|
|
|
|
|
class GiveToggleButtons(object): |
|
|
|
"""Create a window with a pair of toggle buttons.""" |
|
|
|
def __init__(self): |
|
"""Initialize.""" |
|
self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL) |
|
self.window.set_title('Foo') |
|
self.window.connect("delete_event", self.delete_event) |
|
self.window.show() |
|
self.vbox = Gtk.VBox() |
|
self.vbox.show() |
|
self.window.add(self.vbox) |
|
|
|
self.toggle_button1 = Gtk.ToggleButton('Click me 1') |
|
self.toggle_button1.set_name("MyToggleButton") |
|
self.toggle_button1.show() |
|
self.toggle_button1.connect("clicked", self.toggle) |
|
self.vbox.pack_start(self.toggle_button1, True, True, 0) |
|
|
|
self.toggle_button2 = Gtk.ToggleButton('Click me 2') |
|
self.toggle_button2.set_name("MyToggleButton") |
|
self.toggle_button2.show() |
|
self.toggle_button2.connect("clicked", self.toggle) |
|
self.vbox.pack_start(self.toggle_button2, True, True, 0) |
|
|
|
screen = Gdk.Screen.get_default() |
|
cssprovider = Gtk.CssProvider() |
|
cssprovider.load_from_data(CSS) |
|
stylecontext = self.window.get_style_context() |
|
|
|
stylecontext.add_provider_for_screen(screen, cssprovider, |
|
Gtk.STYLE_PROVIDER_PRIORITY_USER) |
|
|
|
Gtk.main() |
|
|
|
@staticmethod |
|
def delete_event(*args, **kwargs): |
|
"""Shut down GTK.""" |
|
make_used(args) |
|
make_used(kwargs) |
|
Gtk.main_quit() |
|
sys.exit(0) |
|
|
|
def toggle(self, widget=None): |
|
"""Called when a button is toggled.""" |
|
if widget is self.toggle_button1: |
|
print('toggled 1') |
|
elif widget is self.toggle_button2: |
|
print('toggled 2') |
|
else: |
|
print('toggled, but what?') |
|
|
|
|
|
GiveToggleButtons() |