'Is there any way to change color of pushButton when a condition(s) satisfies?
I am building an app using Pyqt. It has a few input boxes and a submit button. What I want is that my button should only become active(change colour) when all input boxes are filled. Can I do this ?
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(35, 610, 541, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
Solution 1:[1]
Yes , the idea would be to get the text from all the QTextEdits using
.toPlainText() and color the button if they all are not empty
You should use a thread for checking that.
Here is a quick example of what i mean...
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import threading
global textEdit0
global textEdit1
global textEdit2
global textEdit3
global buttonToColor
def check_input_thread():
while True:
if (textEdit0.toPlainText() != "" and
textEdit1.toPlainText() != "" and
textEdit2.toPlainText() != "" and
textEdit3.toPlainText() != ""):
buttonToColor.setStyleSheet("background-color : green")
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
textEdit0 = QtWidgets.QTextEdit(widget);
textEdit1 = QtWidgets.QTextEdit(widget);
textEdit2 = QtWidgets.QTextEdit(widget);
textEdit3 = QtWidgets.QTextEdit(widget);
buttonToColor = QtWidgets.QPushButton(widget);
textEdit0.move(0, 0)
textEdit1.move(0, 50)
textEdit2.move(0, 100)
textEdit3.move(0, 150)
buttonToColor.move(0, 200)
thread = threading.Thread(target=check_input_thread)
thread.start();
widget.show();
sys.exit(app.exec_())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S.T.A.L.K.E.R |
