In this minimal reproducible example, I have a comboBox and a pushButton. I am trying to activate buttons on the basis of current text selected from the comboBox, but I can't able activate buttons when I tried to verify it first inside if elif else condition, how to activate right function on the basis of current text.
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class MainWindow(QWidget):
def __init__(self):
super(QWidget, self).__init__()
main_layout = QVBoxLayout(self)
self.buttons = []
# Works:
self.combo = QComboBox()
main_layout.addWidget(self.combo)
self.combo.addItems(['PHC'])
self.combo.addItems(['CHC'])
self.combo.addItems(['HSC'])
self.combo.addItems(['DH'])
self.combo.addItems(['LSH'])
# # Connecting comboBox to VerifyFType function
self.combo.currentIndexChanged[str].connect(self.VerifyFType)
self.button_2 = QPushButton('Validate', self)
main_layout.addWidget(self.button_2)
self.buttons.append(self.button_2)
def VerifyFType(self):
print("Entered VerifyFType")
FType = self.combo.currentText()
print(FType)
if(FType == "PHC"):
self.button_2.clicked.connect(lambda: self.PHC_Validate)
elif(FType == "CHC"):
self.button_2.clicked.connect(lambda: self.CHC_Validate)
elif(FType == "DH"):
self.button_2.clicked.connect(lambda: self.DH_Validate)
elif(FType == "HSC"):
self.button_2.clicked.connect(lambda: self.HSC_Validate)
elif(FType == "LSH"):
self.button_2.clicked.connect(lambda: self.LSH_Validate)
else:
"Nothing Matched , No such FType"
def PHC_Validate(self):
print('Entered PHC_Validate')
def CHC_Validate(self):
print('Entered CHC_Validate')
def DH_Validate(self):
print('Entered DH_Validate')
def HSC_Validate(self):
print('Entered HSC_Validate')
def LSH_Validate(self):
print('Entered LSH_Validate')
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())