Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在需要时激活信号_Python_Python 3.x_Pyqt5 - Fatal编程技术网

Python 如何在需要时激活信号

Python 如何在需要时激活信号,python,python-3.x,pyqt5,Python,Python 3.x,Pyqt5,在这个最小的可复制示例中,我有一个组合框和一个按钮。我试图根据从组合框中选择的当前文本激活按钮,但当我试图首先在elif else条件中验证按钮时,我无法激活按钮,即如何根据当前文本激活正确的功能 from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * import sys class MainWindow(QWidget): def __init__(self):

在这个最小的可复制示例中,我有一个组合框和一个按钮。我试图根据从组合框中选择的当前文本激活按钮,但当我试图首先在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_())

您的逻辑是错误的,因为您似乎认为将信号连接到另一个函数将断开信号与前一个函数的连接

解决方案是在按下按钮时使用QComboBox的currentText调用适当的函数

class主窗口(QWidget):
定义初始化(自):
超级(主窗口,自我)。\uuuu初始化
self.combo=QComboBox()
self.combo.addItems([“PHC”、“CHC”、“HSC”、“DH”、“LSH”])
self.button_2=QPushButton(“验证”,self)
self.button_2.单击。连接(self.handle_单击)
主布局=QVBoxLayout(自)
main_layout.addWidget(self.combo)
主布局添加小部件(自身按钮2)
已单击def句柄(自身):
FType=self.combo.currentText()
如果FType==“PHC”:
self.PHC_Validate()
elif FType==“CHC”:
self.CHC_Validate()
elif FType==“DH”:
self.DH_Validate()
elif FType==“HSC”:
self.HSC_Validate()
elif FType==“LSH”:
self.LSH_Validate()
其他:
没有匹配项,没有这样的F类型
def PHC_验证(自身):
打印(“输入PHC_验证”)
def CHC_验证(自身):
打印(“输入CHC\U验证”)
def DH_验证(自我):
打印(“输入DH_验证”)
def HSC_验证(自我):
打印(“输入HSC\U验证”)
def LSH_验证(自):
打印(“输入LSH_验证”)