【问题标题】:pyqt wizard add page on end of wizard (setFinishButtonEarly)pyqt 向导在向导结束时添加页面(setFinishButtonEarly)
【发布时间】:2019-09-30 13:59:44
【问题描述】:

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

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

【问题讨论】:

    标签: python pyqt pyqt4 qwizard


    【解决方案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_())
    

    【讨论】:

    • 哦,伙计,我非常感谢,关于覆盖你的意思是restart 选项?我可以将class Wizard(QtGui.QWizard) 设置为class Wizard(QtGui.QWizardPage) 并将其传递给``` wizard.addpage(Wizard)``` 或者它不起作用?
    • @bahajoher 你知道 OOP 吗?我建议你阅读一下,你自己会回复的。
    • 真的,只是因为我在问一个关于 python 向导的小资源
    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 2020-11-06
    相关资源
    最近更新 更多