2

I made this wizard containing a radio button. When it is clicked, the finish button should return a list of radio buttons that were checked as text!

The input (it's virtual input for readability)

data=[['a','b','c'],['e','f'],['g','f']]
data1 = ['one','two','three']

this is my code

from PyQt4 import QtGui, QtCore

   def page3arg(x, n):
   page = QtGui.QWizardPage()
   page.setTitle("{}".format(x))
   page.setSubTitle("Please choose one of these state.")

   rd1 = QtGui.QRadioButton(page)
   rd2 = QtGui.QRadioButton(page)
   rd3 = QtGui.QRadioButton(page)

   layout = QtGui.QGridLayout()
   layout.addWidget(rd1, 2, 0)
   layout.addWidget(rd2, 2, 1)
   layout.addWidget(rd3, 2, 2)
   rd1.setText(' {}'.format(n[0]))
   rd2.setText(' {}'.format(n[1]))
   rd3.setText(' {}'.format(n[2]))
   page.setLayout(layout)

   return page


def page2arg(x, n):
    page = QtGui.QWizardPage()
    page.setTitle("{}".format(x))
    page.setSubTitle("Please choose one of these state.")

    rd1 = QtGui.QRadioButton(page)
    rd2 = QtGui.QRadioButton(page)

    layout = QtGui.QGridLayout()
    layout.addWidget(rd1, 2, 0)
    layout.addWidget(rd2, 2, 1)
    rd1.setText(' {}'.format(n[0]))
    rd2.setText(' {} .'.format(n[1]))

    page.setLayout(layout)
    return page

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    wizard = QtGui.QWizard()
    wizard.setStyleSheet(("font:50 10pt \"MS Shell Dlg 2\";"))

    for m in range(len(data1) - 1):
        x = data1[m]
        n= data[m]
        if len(n) == 3:
            page3 = page3arg(x, n)
            wizard.addPage(page3)
        elif len(n) == 2:
            page2 = page2arg(x, n)
            wizard.addPage(page2)
    wizard.show()
    sys.exit(wizard.exec_())

How do I write a function that will get the selections of the radio buttons in the end as list.

The list of radio button selections should look like this:

output = ['a','e','g'] 
Towkir
  • 3,889
  • 2
  • 22
  • 41
baha joher
  • 84
  • 8

2 Answers2

1

If you want to get radiobutton checked, then a simple solution is to associate it with a QButtonGroup for each page and use the checkedButton() function to get the option checked. And to know when you press the finish button you must use the button() function of QWizard and connect it to a slot.

Also it is not necessary to have 2 functions that do the same as page3arg and page2arg, I have reduced it in a generalized function for n arguments

from PyQt4 import QtCore, QtGui


class Wizard(QtGui.QWizard):
    def __init__(self, parent=None):
        super(Wizard, self).__init__(parent)
        datas = [["a", "b", "c"], ["e", "f"], ["g", "f"]]
        titles = ["one", "two", "three"]
        self.setStyleSheet(('font:50 10pt "MS Shell Dlg 2";'))

        self.groups = []
        for title, options in zip(titles, datas):
            page, group = Wizard.create_page(title, options)
            self.addPage(page)
            self.groups.append(group)

        self.button(QtGui.QWizard.FinishButton).clicked.connect(
            self.on_finished
        )
        self._results = []

    @property
    def results(self):
        self.get_options()
        return self._results

    def get_options(self):
        self._results = []
        for group in self.groups:
            button = group.checkedButton()
            if button is not None:
                self._results.append(button.text())

    @QtCore.pyqtSlot()
    def on_finished(self):
        print("finished", self.results)


    @staticmethod
    def create_page(title, options):
        page = QtGui.QWizardPage()
        group = QtGui.QButtonGroup(page)
        page.setTitle(title)
        page.setSubTitle("Please choose one of these state.")
        hlay = QtGui.QHBoxLayout(page)
        for option in options:
            radiobutton = QtGui.QRadioButton(text=option)
            group.addButton(radiobutton)
            hlay.addWidget(radiobutton)
        return page, group


if __name__ == "__main__":

    import sys

    app = QtWidgets.QApplication(sys.argv)
    wizard = Wizard()
    wizard.show()
    ret = app.exec_()
    print("outside clas", wizard.results)
    sys.exit(ret)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thanks alot man just question could i get the results out of class with separate attribute – baha joher May 11 '19 at 23:20
  • and another question could i get the same list before click finish button ? – baha joher May 11 '19 at 23:22
  • @bahajoher 1 y 2) Now add the `results` property that returns the selected options: `print(wizard.results)` outside class, `print(self.results)` inside class – eyllanesc May 11 '19 at 23:26
  • about the second question because if i want to print the result in the final page of wizard before the finish button but until here you help me alot so thanks again – baha joher May 11 '19 at 23:33
  • @bahajoher Saying before pressing the finish button is ambiguous, for example it could be interpreted as together before pressing the finish button, or it could also be interpreted as when you pass from the penultimate page to the last one. In the first interpretation it is impossible since you do not know in what moment you press, it would be like guessing the future – eyllanesc May 11 '19 at 23:38
  • aha i understand it you are saint lot of love – baha joher May 12 '19 at 00:24
0

Try to put all radio buttons in a list, and for each radio button in the list, get it's text and insert it to your output list. For example,

lst = []
lst.append(rd1)
lst.append(rd2)
lst.append(rd3)
output = []
for val in lst:
    output.append(val.text())
Amir B
  • 41
  • 6