Radio buttons are good for user input when you want the user to select an exclusive choice from a list of options, as opposed to checkboxes, which let a user select many or all of the options available. For longer lists of choices, a combobox is a better option. Once a radio button is selected, you can unselect it only by choosing another radio button.
Open the QGIS Python Console by selecting the Plugins menu and then clicking on Python Console.
Radio buttons are easier to manage as part of a class, so we'll create a custom class that also includes a textbox to view which radio button is selected. To do this, perform the following steps:
from PyQt4.QtCore import * from PyQt4.QtGui import *
RadioButton
class and set up the radio buttons and the textbox:class RadioButton(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent)
self.layout = QVBoxLayout() self.rb1 = QRadioButton('Option 1') self.rb2 = QRadioButton('Option 2') self.rb3 = QRadioButton('Option 3') self.textbox = QLineEdit()
self.rb1.toggled.connect(self.rb1_active) self.rb2.toggled.connect(self.rb2_active) self.rb3.toggled.connect(self.rb3_active)
self.layout.addWidget(self.rb1) self.layout.addWidget(self.rb2) self.layout.addWidget(self.rb3) self.layout.addWidget(self.textbox)
self.setLayout(self.layout)
def rb1_active(self, on): if on: self.textbox.setText('Option 1 selected') def rb2_active(self, on): if on: self.textbox.setText('Option 2 selected') def rb3_active(self, on): if on: self.textbox.setText('Option 3 selected')
buttons = RadioButton() buttons.show()