PyQt4 - QLineEdit() 和 QCheckbox()

PyQt4 - QLineEdit() and QCheckbox()

我正在构建一个 GUI 来获取用户输入,然后在稍后的某个复杂步骤中使用它们。我有一组复选框,用户必须从中至少选择一个,并在复选框正下方的 QLineEdit 中为其提供一个 'alias' 名称(否则采用默认名称)。

目前,我必须先输入别名,然后在行编辑中选中复选框以注册输入的名称,以获取用户输入的值和连接的复选框名称。这个顺序不正常。 单击 'Continue' 时,有没有办法获取 Editline 数据和连接的复选框名称?

这是我的代码:

from PyQt4 import QtGui, QtCore
import sys

checkpoint_list = ['Amsterdam','Munich','Paris','Mumbai']


class MyGui(QtGui.QWidget):
    def __init__(self):
        super(MyGui, self).__init__()
        self.initUI()
        self.final_list = []
        self.platform_list = {}
        self.qem = None

    def initUI(self):

        lay_out = QtGui.QVBoxLayout(self)

        # select the CPs
        cp_lbl = QtGui.QLabel("Please select CP versions to compare:", self)
        lay_out.addWidget(cp_lbl)
        self.cb = []
        self.platform_label = []

        i = 0
        for cp in checkpoint_list:
            self.cb.append(QtGui.QCheckBox(cp, self))
            self.platform_label.append(QtGui.QLineEdit(cp, self))
            self.cb[i].stateChanged.connect(self.clickBoxStateChanged)
            lay_out.addWidget(self.cb[i])
            lay_out.addWidget(self.platform_label[i])
            i += 1
        lay_out.addStretch(10)

        # Continue and cancel button
        btn_cancel = QtGui.QPushButton('Cancel', self)
        btn_continue = QtGui.QPushButton('Continue', self)

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch()
        hbox.addWidget(btn_continue)
        hbox.addWidget(btn_cancel)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch()

        lay_out.addLayout(hbox)
        lay_out.addLayout(vbox)

        self.setLayout(lay_out)

        btn_cancel.clicked.connect(self.onclick_cancel)
        btn_cancel.setToolTip('To <b>Cancel</b> with this process')

        btn_continue.clicked.connect(self.onclick_Continue)
        btn_continue.setToolTip('To <b>Continue</b> with the matching')

        # Screen show
        self.setGeometry(300, 300, 500, 400)
        self.setWindowTitle('CP Selection Window')
        self.show()

    def clickBoxStateChanged(self, cb):
        self.final_list = []
        self.platform_list = {}
        for i in range(len(self.cb)):
            if self.cb[i].isChecked():
                if self.cb[i] not in self.final_list:
                    self.final_list.append(str(self.cb[i].text()))
                    self.platform_list[str(self.cb[i].text())] = str(self.platform_label[i].text())
                    print self.final_list
                    print self.platform_list
            elif self.cb[i].isChecked() == False:
                if self.cb[i].text() in self.final_list:
                    self.final_list.remove(str(self.cb[i].text()))
                    del self.platform_list[str(self.cb[i].text())]
                    print self.final_list
                    print self.platform_list

    def onclick_Continue(self):
        try:
            if len(self.final_list) == 0:
                self.qem = QtGui.QErrorMessage(self)
                self.qem.showMessage("Please select at least 1 checkpoint to continue...")
            else:
                self.close()
        except:
            print "No CP was selected..."

    def onclick_cancel(self):
        sys.exit()


if __name__ == "__main__":

    # GUI code
    app = QtGui.QApplication(sys.argv)
    w = MyGui()
    app.exec_()

最简单的解决方案是创建一个分析信息的方法,returns 所选元素的字典:

class MyGui(QtGui.QWidget):
    def __init__(self):
        super(MyGui, self).__init__()
        self.initUI()

    def initUI(self):

        lay_out = QtGui.QVBoxLayout(self)

        # select the CPs
        cp_lbl = QtGui.QLabel("Please select CP versions to compare:")
        lay_out.addWidget(cp_lbl)
        self.cb = []
        self.platform_label = []

        for cp in checkpoint_list:
            cb = QtGui.QCheckBox(cp)
            le = QtGui.QLineEdit(cp)
            lay_out.addWidget(cb)
            lay_out.addWidget(le)

            self.cb.append(cb)
            self.platform_label.append(le)

        lay_out.addStretch(10)

        # Continue and cancel button
        btn_cancel = QtGui.QPushButton("Cancel")
        btn_continue = QtGui.QPushButton("Continue")

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch()
        hbox.addWidget(btn_continue)
        hbox.addWidget(btn_cancel)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch()

        lay_out.addLayout(hbox)
        lay_out.addLayout(vbox)

        btn_cancel.clicked.connect(self.onclick_cancel)
        btn_cancel.setToolTip("To <b>Cancel</b> with this process")

        btn_continue.clicked.connect(self.onclick_Continue)
        btn_continue.setToolTip("To <b>Continue</b> with the matching")

        # Screen show
        self.setGeometry(300, 300, 500, 400)
        self.setWindowTitle("CP Selection Window")
        self.show()

    def get_elements_selected(self):
        values_selected = dict()

        for cb, le in zip(self.cb, self.platform_label):
            if cb.isChecked():
                values_selected[cb.text()] = le.text()
        return values_selected

    def onclick_Continue(self):
        values = self.get_elements_selected()
        if values:
            print(values)
            self.close()
        else:
            qem = QtGui.QErrorMessage(self)
            qem.showMessage("Please select at least 1 checkpoint to continue...")
            qem.exec_()

    def onclick_cancel(self):
        sys.exit()