PyQt4 - QLineEdit() 和 QCheckbox()

Posted

技术标签:

【中文标题】PyQt4 - QLineEdit() 和 QCheckbox()【英文标题】:PyQt4 - QLineEdit() and QCheckbox() 【发布时间】:2021-02-26 17:38:47 【问题描述】:

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

目前,我必须先输入别名,然后选中复选框以在行编辑中注册输入的名称,以获取用户输入的值和连接的复选框名称。这个顺序不正常。 单击“继续”时,有没有办法获取 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_()

【问题讨论】:

如果您只需要在按“继续”时检查这些值,则无需在每次切换项目时不断重建列表:只需将 clickBoxStateChanged 中的所有内容移动到函数中与点击的信号相连。 @musicamante : 谢谢 :) 它也解决了这个问题,但现在要选择至少一个检查点的条件需要更多代码 【参考方案1】:

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

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

【讨论】:

非常感谢您提供如此干净的代码!它解决了我的问题:)

以上是关于PyQt4 - QLineEdit() 和 QCheckbox()的主要内容,如果未能解决你的问题,请参考以下文章

如何将 QLineEdit 放入 QTableView 单元格 PyQt4?

PyQt4 跨 .py 文件共享 QLineEdit 变量

python PyQt4中如何获得QTextEdit的内容获得QLineEdit的内容有QLineEdit.text(),那QTextEdit呢?

使用 pyqt4 创建类信号

PyQT4:从 Ui 获取现有 python 程序的文本

PyQt4表达式求值程序