Ir para o conteúdo

Python e GTK 3: PyGObject Gtk.RadioButton()

Utilizando Gtk.RadioButton() com a linguagem de programação Python (PyGObject).

Exemplo

Python PyGObject Gtk.RadioButton()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""Python e GTK 3: PyGObject Gtk.RadioButton()."""

import gi

gi.require_version(namespace='Gtk', version='3.0')
from gi.repository import Gtk, Gio


class MainWindow(Gtk.ApplicationWindow):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.set_title(title='Gtk.RadioButton')
        self.set_default_size(width=1366 / 2, height=768 / 2)
        self.set_position(position=Gtk.WindowPosition.CENTER)
        self.set_default_icon_from_file(filename='../../assets/icons/icon.png')

        button_box = Gtk.ButtonBox.new(orientation=Gtk.Orientation.VERTICAL)
        button_box.set_layout(Gtk.ButtonBoxStyle.CENTER)
        button_box.set_spacing(spacing=6)
        button_box.set_border_width(border_width=12)
        self.add(widget=button_box)

        radio_button_01 = Gtk.RadioButton.new_with_label(
            group=None,
            label='Radio button 01',
        )
        radio_button_01.connect('toggled', self.on_radiobutton_toggled)
        button_box.add(widget=radio_button_01)

        radio_button_02 = Gtk.RadioButton.new_with_label_from_widget(
            radio_group_member=radio_button_01,
            label='Radio button 02',
        )
        radio_button_02.connect('toggled', self.on_radiobutton_toggled)
        button_box.add(widget=radio_button_02)

        radio_button_03 = Gtk.RadioButton.new_with_label_from_widget(
            radio_group_member=radio_button_01,
            label='Radio button 03',
        )
        radio_button_03.connect('toggled', self.on_radiobutton_toggled)
        button_box.add(widget=radio_button_03)

        radio_button_04 = Gtk.RadioButton.new_with_label_from_widget(
            radio_group_member=radio_button_01,
            label='Radio button 04',
        )
        radio_button_04.connect('toggled', self.on_radiobutton_toggled)
        button_box.add(widget=radio_button_04)

        self.show_all()

    def on_radiobutton_toggled(self, widget):
        if widget.get_active():
            print(f'{widget.get_label()} - MARCADO')
        else:
            print(f'{widget.get_label()} - DESMARCADO')


class Application(Gtk.Application):

    def __init__(self):
        super().__init__(application_id='br.natorsc.Exemplo',
                         flags=Gio.ApplicationFlags.FLAGS_NONE)

    def do_startup(self):
        Gtk.Application.do_startup(self)

    def do_activate(self):
        win = self.props.active_window
        if not win:
            win = MainWindow(application=self)
        win.present()

    def do_shutdown(self):
        Gtk.Application.do_shutdown(self)


if __name__ == '__main__':
    import sys

    app = Application()
    app.run(sys.argv)