python - 如何在选中复选框时启用行编辑

标签 python pyside

我需要在 Python 中创建一个宏。

我想在选中 QCheckBox 时启用 QlineEdit;我该怎么做?

这是代码:

import os, sys, App
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.fabricatorLineEdit = self.createLineEdit("fabricatorLineEdit", "fabricator")
        self.fabricatorLineEdit.setDisabled(True)

        self.chBoxFab = self.createCheckBox("chBoxFab", "Insert a alternative value to 'fabricator' key:")
        self.chBoxFab.stateChanged.connect(self.chbxStateChange(self.chBoxFab, self.fabricatorLineEdit))

        self.chBoxPrjManager = self.createCheckBox("chBoxPrjManager", "Insert a alternative value to 'Project Manager' key:")

        self.projManagerLineEdit = self.createLineEdit("projManagerLineEdit", "Project Manager")
        self.projManagerLineEdit.setDisabled(True)  
      self.chBoxPrjManager.stateChanged.connect(self.chbxStateChange(self.chBoxPrjManager , self.projManagerLineEdit))
        mainLayout = QtWidgets.QFormLayout()
        mainLayout.addRow(self.chBoxFab)
        mainLayout.addRow(self.fabricatorLineEdit)
        mainLayout.addRow(self.chBoxPrjManager)
        mainLayout.addRow(self.projManagerLineEdit)
        self.setLayout(mainLayout)

    def createLineEdit(self, objName, defaultTxt):
        lineEdit = QtWidgets.QLineEdit(self)
        lineEdit.setObjectName(objName)
        lineEdit.setText(defaultTxt)
        return lineEdit

    def createCheckBox(self, objName, objCaption):
        chBox = QtWidgets.QCheckBox(self)
        chBox.setObjectName(objName)
        chBox.setText(objCaption)
        return chBox

    def chbxStateChange(self, chBox, lineEdit):
       if chBox.isChecked:
        lineEdit.setDisabled(False)
       else:
          lineEdit.setDisabled(True)

    if __name__ == '__main__':

       import sys

       window = Window()
       result = window.exec_()

我定义了一个事件来检查QCheckBox的状态以启用QlineEdit,但它不起作用:

    def chbxStateChange(self, chBox, lineEdit):
        if chBox.isChecked:
            lineEdit.setDisabled(False)
        else:
            lineEdit.setDisabled(True)

我知道问题出在函数 chbxStateChange 中,但我不知道如何解决它。

错误:

error return without exception set Traceback (most recent call last): File "C:/Program Files/.../Python/Scripts/test.py", line 332, in window = Window() File "C:/Program Files/.../Python/Scripts/test.py", line 35, in init

self.chBoxFab.stateChanged.connect(self.chbxStateChange(self.chBoxFab, self.fabricatorLineEdit))

最佳答案

您的代码中有两个错误。

首先,connect信号方法接受一个可调用对象,但您的代码传入方法的返回值(在本例中为 None )。因此,您应该像这样建立连接:

self.chBoxFab.stateChanged.connect(
    lambda: self.chbxStateChange(self.chBoxFab,
                                 self.fabricatorLineEdit))

self.chBoxPrjManager.stateChanged.connect(
    lambda: self.chbxStateChange(self.chBoxPrjManager,
                                 self.projManagerLineEdit))

其次,chbxStateChange方法犯了与治疗相反的错误isChecked作为属性(而不是调用它),这意味着它将始终评估为 True 。因此,代码应该如下所示:

def chbxStateChange(self, chBox, lineEdit):
    if chBox.isChecked():
        lineEdit.setDisabled(False)
    else:
        lineEdit.setDisabled(True)

但是,如果您只想切换行编辑的启用状态,则此代码过于复杂。连接可以这样建立:

self.chBoxFab.toggled.connect(self.fabricatorLineEdit.setEnabled)
self.chBoxPrjManager.toggled.connect(self.projManagerLineEdit.setEnabled)

toggled signal发送按钮的选中状态( TrueFalse ),然后可以直接设置行编辑的启用状态。 (如果您希望在选中按钮时禁用行编辑,则可以将 toggled 连接到 setDisabled )。

如果您这样做,chbxStateChange不再需要方法。

<小时/>

(注意: QCheckBox 类继承 QAbstractButton ,这是它获取 toggled 信号的地方)。

关于python - 如何在选中复选框时启用行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41702852/

相关文章:

python - Pandas 数据透视表中的多维乘法

python - python3上的scrapy如何获取在javascript上工作​​的文本数据

python - 如何在 matplotlib 表中设置列的背景颜色

python - 带信号/槽的 PySide IPC

qt - 获取光标下的所有小部件

python - 在python中将空格替换为+

python - 使用 css 选择器查找标签,但不查找其后代

python - 除了继承之外,在 `super() ` 内部使用 python `__init__ ` 的目的是什么?

python3.3在linux中找不到libpython3.3m.so(pip-3.3)

python - Sqlite3 或 QtSql