Blame platform-demos/gl/radiobutton.py.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="radiobutton.py" xml:lang="gl">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">RadioButton (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#buttons"/>
Packit 1470ea
    <link type="seealso" xref="grid.py"/>
Packit 1470ea
    <link type="next" xref="buttonbox.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-05-09" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>Botóns mutuamente exclusivos.</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Fran Dieguez</mal:name>
Packit 1470ea
      <mal:email>frandieguez@gnome.org</mal:email>
Packit 1470ea
      <mal:years>2012-2013.</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>RadioButton</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/radiobutton.png"/>
Packit 1470ea
  

Three RadioButtons. You can see in the terminal if they are turned off or on.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Código usado para xerar este exemplo</title>
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)
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
    <title>Métodos útiles para o widget RadioButton</title>
Packit 1470ea
    

In line 16 the signal "toggled" is connected to the callback function toggled_cb() using widget.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

Packit 1470ea
Packit 1470ea
    

As seen in <link xref="properties.py"/>, instead of button1 = Gtk.RadioButton(label="Button 1") we could create the button and label it with

Packit 1470ea
    
Packit 1470ea
button1 = Gtk.RadioButton()
Packit 1470ea
button1.set_label("Button 1").
Packit 1470ea
    

Yet another way to create a new RadioButton with a label is button1 = Gtk.RadioButton.new_with_label(None, "Button 1") (the first argument is the group of the radiobuttons, which we can get with get_group(), the second argument is the label).

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API References</title>
Packit 1470ea
    

Neste exemplo empregaremos o seguinte:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkWindow.html">GtkWindow</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkGrid.html">GtkGrid</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkRadioButton.html">GtkRadioButton</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>