【发布时间】:2021-06-11 15:22:35
【问题描述】:
在这个最小的可重现示例中,我有一个组合框和一个按钮。我正在尝试根据从组合框中选择的当前文本激活按钮,但是当我尝试在 elif else 条件下首先验证它时,我无法激活按钮,如何根据当前文本激活正确的功能。
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_())
【问题讨论】:
标签: python python-3.x pyqt5