Skip to main content
 首页 » 编程设计

python之PyQt 使用向导和单选按钮

2024年11月24日8del

我制作了这个包含单选按钮的向导。单击它时,完成按钮应该返回一个单选按钮列表,这些单选按钮被选中为文本!

输入(为了便于阅读,它是虚拟输入)

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

这是我的代码

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_()) 

我如何编写一个函数,最终将单选按钮的选择作为列表。

单选按钮选择列表应如下所示:

output = ['a','e','g']  

请您参考如下方法:

如果你想检查单选按钮,那么一个简单的解决方案是将它与 QButtonGroup 相关联对于每个页面并使用 checkedButton()函数来检查选项。要知道何时按下完成按钮,您必须使用 button() QWizard 的功能并将其连接到插槽。

也没有必要有 2 个函数与 page3arg 和 page2arg 做同样的事情,我已经将它简化为 n 个参数的通用函数

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)