PyQt Wizard добавить страницу в конце мастера (setFinishButtonEarly) - PullRequest
2 голосов
/ 13 мая 2019

Я ищу ресурс, чтобы увидеть какой-то метод в pyqt4.

У меня есть finalPage, который получает аргумент от мастера классов, и хочу показать какой-то результат, основанный на этом аргументе, и кнопка завершения должна быть в последнем из мастера классов, а finalPage получила только закрытие кнопка.

мой код

from PyQt4 import QtCore, QtGui
import sys
from ex import dbconnect
data = [['Male', 0, 'cheap', 'Low', 'Bus'], ['Male', 1, 'cheap', 'Medium', 'Bus'], ['Female', 1, 'cheap', 'Medium', 'Train'], ['Female', 0, 'cheap', 'Low', 'Bus'], ['Male', 1, 'cheap', 'Medium', 'Bus'], ['Male', 0, 'standard', 'Medium', 'Train'], ['Female', 1, 'standard', 'Medium', 'Train'], ['Female', 1, 'expensive', 'High', 'Car'], ['Male', 2, 'expensive', 'Medium', 'Car'], ['Female', 2, 'expensive', 'High', 'Car']]
data1 = ['Gender', 'car ownership', 'Travel Cost', 'Income Level', 'Transportation mode']

#this class i got helped with it

class Wizard(QtGui.QWizard):

   def __init__(self):
       super(Wizard, self).__init__()
       datas = []
       for l in range(len(data1) - 2):
           y = []
           for row in data:
               x = row[l]
               if not x in y:
                   y.append(x)
           datas.append(y)
       titles = data1
       self.setStyleSheet(('font:50 10pt "MS Shell Dlg 2";'))
       self.setWindowTitle("Recommender System")

       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 = []
       self.show()

   @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=str(option))
           group.addButton(radiobutton)
           hlay.addWidget(radiobutton)
       return page, group

########################################################################################################################
#this passed from the class like the r = Wizard.results()

def finalpage(r):     
   page = QtGui.QWizardPage()
   page.setTitle("Conclusion!")

   list1 = QtGui.QListView()
   label2 = QtGui.QLabel('Based on these Ruls')
   label2.setStyleSheet(("font:50 10pt \"MS Shell Dlg 2\";"))
   model = QtGui.QStandardItemModel(list1)
   for m in r:
       item = QtGui.QStandardItem(str(m))
       model.appendRow(item)
       list1.setModel(model)
   layout = QtGui.QVBoxLayout()
   layout.addWidget(list1)
   page.setLayout(layout)
   return page

def run1():

   global data, data1
   app = QtGui.QApplication(sys.argv)
   if app is None:
       app = QtGui.QApplication([])
   wizard = Wizard()
   wizard.addpage(finalpage(wizard.results())
   sys.exit(app.exec_())
run1()

Я не знаю, где и когда позвонить на последнюю страницу

1 Ответ

1 голос
/ 14 мая 2019

Если вы хотите получить информацию из QWizard или QWizardPage перед сменой страниц, вы должны переопределить метод initializePage.

from PyQt4 import QtCore, QtGui

data = [
    ["Male", 0, "cheap", "Low", "Bus"],
    ["Male", 1, "cheap", "Medium", "Bus"],
    ["Female", 1, "cheap", "Medium", "Train"],
    ["Female", 0, "cheap", "Low", "Bus"],
    ["Male", 1, "cheap", "Medium", "Bus"],
    ["Male", 0, "standard", "Medium", "Train"],
    ["Female", 1, "standard", "Medium", "Train"],
    ["Female", 1, "expensive", "High", "Car"],
    ["Male", 2, "expensive", "Medium", "Car"],
    ["Female", 2, "expensive", "High", "Car"],
]
data1 = [
    "Gender",
    "car ownership",
    "Travel Cost",
    "Income Level",
    "Transportation mode",
]


class Wizard(QtGui.QWizard):
    def __init__(self):
        super(Wizard, self).__init__()
        datas = []
        for l in range(len(data1) - 2):
            y = []
            for row in data:
                x = row[l]
                if not x in y:
                    y.append(x)
            datas.append(y)
        titles = data1
        self.setStyleSheet(('font:50 10pt "MS Shell Dlg 2";'))
        self.setWindowTitle("Recommender System")

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

        final_page = FinalPage()
        self.addPage(final_page)

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

    def get_options(self):
        results = []
        for group in self.groups:
            button = group.checkedButton()
            if button is not None:
                results.append(button.text())
        return 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=str(option))
            group.addButton(radiobutton)
            hlay.addWidget(radiobutton)
        return page, group


class FinalPage(QtGui.QWizardPage):
    def __init__(self, parent=None):
        super(FinalPage, self).__init__(parent)
        self.setTitle("Conclusion!")

        self._model = QtGui.QStandardItemModel(self)

        listview = QtGui.QListView()
        listview.setModel(self._model)
        label = QtGui.QLabel("Based on these Ruls")
        label.setStyleSheet(('font:50 10pt "MS Shell Dlg 2";'))

        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(listview)
        lay.addWidget(label)

    def setResults(self, r):
        for m in r:
            item = QtGui.QStandardItem(str(m))
            self._model.appendRow(item)

    def initializePage(self):
        self.setResults(self.wizard().results)


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    w = Wizard()
    w.show()
    sys.exit(app.exec_())
...