简体   繁体   中英

setEnabled(True/False) -> QPushButton when multiple QLineEdit are not empty

I have a gui application with 3 QLineEdit and 2 QPushButton.

I want to put on 'True' the 'Button 1' when 'TEXT INPUT A' and 'TEXT INPUT B' are not empty, and the same on 'Button 2' if all 3 'TEXT INPUT' are not empty.

If all 3 'TEXT INPUT' have the text, buttons are 'True', if I delete for example the text in the 'TEXT INPUT C', the 'Button 2' return to "False".

Same for 'Button 1' if 'TEXT INPUT A' or 'TEXT INPUT B' return empty.

Any suggestions?

import PyQt4.QtGui as gui, PyQt4.QtCore as core

app = gui.QApplication([])
window = gui.QWidget()
window.resize(256, 0)
window.setWindowTitle('MY TITLE')
layout = gui.QVBoxLayout()


A_edit = gui.QLineEdit() 
A_title = gui.QLabel('TEXT INPUT A')
A_edit.setObjectName('TITLE_A')
layout.addWidget(A_title)
layout.addWidget(A_edit)

B_edit = gui.QLineEdit() 
B_title = gui.QLabel('TEXT INPUT B')
B_edit.setObjectName('TITLE_B')
layout.addWidget(B_title)
layout.addWidget(B_edit)

C_edit = gui.QLineEdit() 
C_title = gui.QLabel('TEXT INPUT C')
C_edit.setObjectName('TITLE_C')
layout.addWidget(C_title)
layout.addWidget(C_edit)

button_1 = gui.QPushButton('button 1')
button_2 = gui.QPushButton('button 2')

layout.addWidget(button_1)
layout.addWidget(button_2)
button_1.setEnabled(False)
button_2.setEnabled(False)

window.setLayout(layout)
window.show()

app.exec_()

you could connect the 3 lineedits to a function

A_edit.textChanged.connect(check_buttons)
B_edit.textChanged.connect(check_buttons)
C_edit.textChanged.connect(check_buttons)

And in the check_buttons function you check each lineEdit

check_buttons():
    a = A_edit.text()
    b = B_edit.text()
    c = C_edit.text()
    if a and b:
        button_1.setEnabled(True)
    else:
        button_1.setEnabled(False)
    if a and b and c:
        button_2.setEnabled(True)
    else:
        button_2.setEnabled(False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM