简体   繁体   English

Python-方法被调用的次数超过应有的次数

[英]Python - Method is called more times than it should

I am developing an application using PyQt. 我正在使用PyQt开发应用程序。 I have different push buttons that are connected to the method subeventos . 我有连接到方法subeventos不同按钮。 When that method is called, it fetches data from a database (the data depends on the button clicked), organizes said data in a list and replaces several radio buttons' names with the string of each element from that list. 调用该方法时,它将从数据库中获取数据(数据取决于单击的按钮),将所述数据组织在一个列表中,并用该列表中每个元素的字符串替换几个单选按钮的名称。 Those radio buttons were created in Qt-Designer. 这些单选按钮是在Qt-Designer中创建的。

When a radio button is checked (toggled) it connects to the method radio_clicked . 选中(切换)单选按钮后,它会连接到radio_clicked方法。

The first time I start the app and check a radio button it calls the method radio_clicked once, as it should. 首次启动应用程序并检查单选按钮时,它会按需要调用一次radio_clicked方法。 After, if I select a different radio button, it now calls the method radio_clicked twice and it shouldn't. 之后,如果我选择其他单选按钮,则它现在将方法radio_clicked调用两次,而不应该调用两次。 If I go back and select a different push button and select a random radio button it will call the method radio_clicked 4 times, and so on. 如果我返回并选择另一个按钮,然后选择一个随机单选按钮,它将调用radio_clicked方法4次,依此类推。 This is not desirable, that method should always be called only once. 这是不希望的,该方法应始终仅被调用一次。

The code is below. 代码如下。 Can you help me understand why this happens and how to avoid it? 您能帮助我理解为什么会发生这种情况以及如何避免这种情况吗? Thank you. 谢谢。

    def subeventos(self, evento):
        self.set_tab(4)

        subeventos=["NULL","NULL","NULL","NULL","NULL","NULL","NULL"] #lista nula
        query="SELECT subeventos from eventos_e_subeventos WHERE evento='"+evento+"';"
        cur.execute(query)
        subeventos_fetch=str(cur.fetchall()).strip("[(u',')]").decode('unicode-escape')
        subeventos_split=subeventos_fetch.split(",")

        for i, subevento in enumerate(subeventos_split):
            subeventos[i]=subevento


        for i,subevento in enumerate(subeventos): #Apresente o nome dos subeventos nos RadioButtons dinamicamente
            radiobutton="R"+str(i+1)
            if subevento=="NULL": #Se não exisitr subevento, não mostrar botão
                getattr(self, radiobutton).setVisible(False)
            else:
                getattr(self, radiobutton).setVisible(True)
                getattr(self, radiobutton).setText(subevento)
                getattr(self, radiobutton).toggled.connect(lambda: self.radio_clicked(evento))



    def radio_clicked(self, evento):
        print "I got here!"
        radiobutton=str(self.sender().objectName())
        subevento=getattr(self,radiobutton).text()
        self.BotaoConfirmarSubeventos.clicked.connect(lambda: self.update_database(evento, subevento))

Are the radio_clicked calls coming from the same radiobutton? 单选按钮是否来自同一单选按钮? or different radiobuttons? 或其他单选按钮? The toggle.connect signal will trigger whenever it is switched on or off, not just on. toggle.connect信号将在每次打开或关闭时触发,而不仅仅是打开。 So if you have two radio buttons with one selected, when you click the other radio buttons, it will send two toggle.connect, one for radiobutton1 and one for radiobutton2, not just the one that has been checked. 因此,如果您选择了两个单选按钮,则单击其他单选按钮时,它将发送两个toggle.connect,一个用于单选按钮1,一个用于单选按钮2,而不仅仅是已选中的按钮。 If that is the problem, you can add 如果那是问题,您可以添加

if self.radiobutton.isChecked():
    subevento=getattr(self,radiobutton).text()
    self.BotaoConfirmarSubeventos.clicked.connect(lambda: self.update_database(evento, subevento))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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