Skip to main content
 首页 » 编程设计

python之pyqt 向导在向导结束时添加页面(setFinishButtonEarly)

2024年11月24日8落叶无声

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

不知道何时何地调用最终页面

请您参考如下方法:

如果要在更改页面之前从 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_())