Blame platform-demos/C/samples/radiobutton.py

Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="RadioButton Example", application=app)
Packit 1470ea
        self.set_default_size(250, 100)
Packit 1470ea
        self.set_border_width(20)
Packit 1470ea
Packit 1470ea
        # a new radiobutton with a label
Packit 1470ea
        button1 = Gtk.RadioButton(label="Button 1")
Packit 1470ea
        # connect the signal "toggled" emitted by the radiobutton
Packit 1470ea
        # with the callback function toggled_cb
Packit 1470ea
        button1.connect("toggled", self.toggled_cb)
Packit 1470ea
Packit 1470ea
        # another radiobutton, in the same group as button1
Packit 1470ea
        button2 = Gtk.RadioButton.new_from_widget(button1)
Packit 1470ea
        # with label "Button 2"
Packit 1470ea
        button2.set_label("Button 2")
Packit 1470ea
        # connect the signal "toggled" emitted by the radiobutton
Packit 1470ea
        # with the callback function toggled_cb
Packit 1470ea
        button2.connect("toggled", self.toggled_cb)
Packit 1470ea
        # set button2 not active by default
Packit 1470ea
        button2.set_active(False)
Packit 1470ea
Packit 1470ea
        # another radiobutton, in the same group as button1,
Packit 1470ea
        # with label "Button 3"
Packit 1470ea
        button3 = Gtk.RadioButton.new_with_label_from_widget(
Packit 1470ea
            button1, "Button 3")
Packit 1470ea
        # connect the signal "toggled" emitted by the radiobutton
Packit 1470ea
        # with the callback function toggled_cb
Packit 1470ea
        button3.connect("toggled", self.toggled_cb)
Packit 1470ea
        # set button3 not active by default
Packit 1470ea
        button3.set_active(False)
Packit 1470ea
Packit 1470ea
        # a grid to place the buttons
Packit 1470ea
        grid = Gtk.Grid.new()
Packit 1470ea
        grid.attach(button1, 0, 0, 1, 1)
Packit 1470ea
        grid.attach(button2, 0, 1, 1, 1)
Packit 1470ea
        grid.attach(button3, 0, 2, 1, 1)
Packit 1470ea
        # add the grid to the window
Packit 1470ea
        self.add(grid)
Packit 1470ea
Packit 1470ea
    # callback function
Packit 1470ea
    def toggled_cb(self, button):
Packit 1470ea
        # a string to describe the state of the button
Packit 1470ea
        state = "unknown"
Packit 1470ea
        # whenever the button is turned on, state is on
Packit 1470ea
        if button.get_active():
Packit 1470ea
            state = "on"
Packit 1470ea
        # else state is off
Packit 1470ea
        else:
Packit 1470ea
            state = "off"
Packit 1470ea
        # whenever the function is called (a button is turned on or off)
Packit 1470ea
        # print on the terminal which button was turned on/off
Packit 1470ea
        print(button.get_label() + " was turned " + state)
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)