After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 684865 - Update code from http://developer.gnome.org/gnome-devel-demos/unstable/radiobutton.py.html.en to python 3
Update code from http://developer.gnome.org/gnome-devel-demos/unstable/radiob...
Status: RESOLVED DUPLICATE of bug 723092
Product: gnome-devel-docs
Classification: Applications
Component: platform-demos
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gnome-devel-docs maintainers
gnome-devel-docs maintainers
Depends on:
Blocks: python3
 
 
Reported: 2012-09-26 11:47 UTC by Carlos Soriano
Modified: 2014-01-27 11:45 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Carlos Soriano 2012-09-26 11:47:17 UTC
Fixed code:

from gi.repository import Gtk
import sys

class MyWindow(Gtk.ApplicationWindow):
    def __init__(self, app):
        Gtk.Window.__init__(self, title="RadioButton Example", application=app)
        self.set_default_size(250, 100)
        self.set_border_width(20)

        # a new radiobutton with a label
        button1 = Gtk.RadioButton(label="Button 1")
        # connect the signal "toggled" emitted by the radiobutton
        # with the callback function toggled_cb
        button1.connect("toggled", self.toggled_cb)

        # another radiobutton, in the same group as button1
        button2 = Gtk.RadioButton.new_from_widget(button1)
        # with label "Button 2"
        button2.set_label("Button 2")
        # connect the signal "toggled" emitted by the radiobutton
        # with the callback function toggled_cb
        button2.connect("toggled", self.toggled_cb)
        # set button2 not active by default
        button2.set_active(False)

        # another radiobutton, in the same group as button1,
        # with label "Button 3"
        button3 = Gtk.RadioButton.new_with_label_from_widget(button1, "Button 3")
        # connect the signal "toggled" emitted by the radiobutton
        # with the callback function toggled_cb
        button3.connect("toggled", self.toggled_cb)
        # set button3 not active by default
        button3.set_active(False)

        # a grid to place the buttons
        grid = Gtk.Grid.new()
        grid.attach(button1, 0, 0, 1, 1);
        grid.attach(button2, 0, 1, 1, 1);
        grid.attach(button3, 0, 2, 1, 1);
        # add the grid to the window
        self.add(grid)

    # callback function
    def toggled_cb(self, button):
        # a string to describe the state of the button
        state = "unknown"
        # whenever the button is turned on, state is on
        if button.get_active():
            state = "on"
        # else state is off
        else:
            state = "off"
        # whenever the function is called (a button is turned on or off)
        # print on the terminal which button was turned on/off
        
        print(button.get_label() + " was turned " + state)

class MyApplication(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = MyWindow(self)
        win.show_all()

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

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
Comment 1 André Klapper 2012-09-26 11:49:10 UTC
Thanks! Does this code still work in Python 2?

We plan to use Python3 for GNOME 3.8 (March 2013).
Comment 2 Carlos Soriano 2012-09-26 11:51:09 UTC
No, it doesn't. So I wait until gnome 3.8 to report bugs against python 3 port =)
Comment 3 David King 2014-01-27 11:45:52 UTC

*** This bug has been marked as a duplicate of bug 723092 ***