我有两个仅接受数字值的行编辑(le_A
和le_B
)和一个复选框(chk_box
)。每当检查le_A
(其中le_B
是“控制器”)时,我都无法使chk_box
和chk_box
具有相同的输入(请参见下面的方案2)。
示例场景:
情况1.未选中时,用户可以在le_A
或le_B
中输入任何值。例如,le_A
中的值为10,而le_B
中的值为20。
情况2。选中时,用户在le_A
和le_B
中输入的任何值都将相同。例如,如果我在le_A
中输入10,则le_B
将为10。在le_B
中进行的输入也一样-le_A
中将显示相同的值。
代码:
class CustomTest(QtGui.QWidget):
def __init__(self, parent=None):
super(CustomTest, self).__init__(parent)
# Only numeric values
self.le_A = QtGui.QLineEdit()
self.le_B = QtGui.QLineEdit()
self.chk_box = QtGui.QCheckBox()
lyt = QtGui.QHBoxLayout()
lyt.addWidget(self.le_A)
lyt.addWidget(self.le_B)
lyt.addWidget(self.chk_box)
self.setLayout(lyt)
self.set_connections()
def set_connections(self):
self.chk_box.stateChanged.connect(self.chk_toggle)
def chk_toggle(self):
chk_value = self.chk_box.isChecked()
a_val = self.le_A.text()
b_val = self.le_B.text()
# Inputs in either le_A and le_B should be the same
if chk_value:
# If the values are different, always use a_val as the base value
if a_val != b_val:
self.le_B.setText(str(b_val))
else:
# Inputs in either le_A and le_B can be different
# Currently this is working
pass
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = CustomTest()
w.show()
sys.exit(app.exec_())
我有两个仅包含数值的行编辑(le_A和le_B)和一个复选框(chk_box)。每当...