2

我正在寻找资源以查看 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()

我不知道何时何地调用最后一页

4

1 回答 1

1

如果要在更改页面之前从 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_())
于 2019-05-14T01:25:43.500 回答