当前位置: 首页 > 面试题库 >

pyQt:radioButton.isChecked()被执行两次

齐起运
2023-03-14
问题内容

我有一个从Qt Designer派生的简单窗口 (design.py) ,其中包含三个单选按钮:

# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.setEnabled(True)
        MainWindow.resize(158, 110)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.myradioButton1 = QtGui.QRadioButton(self.centralwidget)
        self.myradioButton1.setGeometry(QtCore.QRect(20, 10, 102, 22))
        self.myradioButton1.setObjectName(_fromUtf8("myradioButton1"))
        self.myradioButton2 = QtGui.QRadioButton(self.centralwidget)
        self.myradioButton2.setGeometry(QtCore.QRect(20, 40, 102, 22))
        self.myradioButton2.setObjectName(_fromUtf8("myradioButton2"))
        self.myradioButton3 = QtGui.QRadioButton(self.centralwidget)
        self.myradioButton3.setGeometry(QtCore.QRect(20, 70, 102, 22))
        self.myradioButton3.setObjectName(_fromUtf8("myradioButton3"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.myradioButton1.setText(_translate("MainWindow", "RadioButton1", None))
        self.myradioButton2.setText(_translate("MainWindow", "RadioButton2", None))
        self.myradioButton3.setText(_translate("MainWindow", "RadioButton3", None))

并且我添加了此代码,以便监视选中了哪个单选按钮。

# -*- coding: utf-8 -*-

from PyQt4 import QtGui, QtCore
import sys
import design

class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.setupUi(self)

        self.myradioButton1.toggled.connect(self.myradioButton1_function) 
        self.myradioButton2.toggled.connect(self.myradioButton1_function) 
        self.myradioButton3.toggled.connect(self.myradioButton1_function)

    def myradioButton1_function(self):
        if self.myradioButton1.isChecked():
            print 'myradioButton1 is Checked'        
        if self.myradioButton2.isChecked():
            print 'myradioButton2 is Checked'        
        if self.myradioButton3.isChecked():
            print 'myradioButton3 is Checked'

def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

我注意到,如果选中radioButton1,它似乎可以正常工作,但是如果选中radiobutton2或radiobutton3,则将检查消息打印两次。

另一方面,如果我将每个信号都连接到不同的功能,例如:

class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.setupUi(self)

        self.myradioButton1.toggled.connect(self.myradioButton1_function) 
        self.myradioButton2.toggled.connect(self.myradioButton2_function) 
        self.myradioButton3.toggled.connect(self.myradioButton3_function)

    def myradioButton1_function(self):
        if self.myradioButton1.isChecked():
            print 'myradioButton1 is Checked'

    def myradioButton2_function(self):
        if self.myradioButton2.isChecked():
            print 'myradioButton2 is Checked'

    def myradioButton3_function(self):
        if self.myradioButton3.isChecked():
            print 'myradioButton3 is Checked'

然后它会按预期工作。

因此,我想将多个信号连接到一个功能时会发生麻烦。有人可以解释这种行为吗?

任何想法将不胜感激。


问题答案:

toggle()每当 任何 单选按钮的状态更改时, 都会
发出该信号。结果,toggle()当您单击单选按钮时,该信号被发射,并且该单选按钮的状态从未选中变为已选中,并且如果单击该单选按钮会自动取消选中另一个单选按钮,则该toggle()信号将再次发射,因为另一个单选按钮的状态从选中变为未选中。

您可以通过在插槽的末尾添加以下行来查看实际情况:

print self.sender().text() + ' was toggled'

clicked()改为使用信号-从未单击过状态自动从已选中变为未选中的单选按钮。



 类似资料:
  • 问题内容: 我上面的代码作为Spark驱动程序,当我执行程序时,它可以正常工作,将所需数据保存为Parquet文件。 但是我观察到我在RDD上的映射器函数被执行两次。首先,当我读为使用 第二时,当我将其写入 实木复合地板文件时 您能指导我如何避免这种重复执行吗?还有其他更好的方法将JSON字符串转换为Dataframe吗? 问题答案: 我认为原因是JSON阅读器缺少架构。执行时: Spark必须为

  • 从HomeActivity我正在尝试从CreateProfileActivity获取结果。在这里我要做什么来开始这个活动 以下是HomeActivity中方法的实现: 当im完成并按下save in后,下面是我将数据发送回HomeActivity的操作: 在上调用setResult方法一次,但由于某种未知原因,当数据到达方法时,get execute两次。第一个结果的和第二个结果的。在此之后,得到

  • 我的建议是正确执行并预先准备正确的操作,除了执行两次。我希望它只执行一次。应该触发通知的方法只执行一次,因为startTestSuite标题只在日志中打印一次。bean和上下文是在TestNG类中生成的。我尝试在initSpring()方法上使用@beforeClass和@beforeSuite标记运行它,结果相同。 进一步的上下文:这样做的目的是获取测试套件何时开始和结束的时间戳,以及各个测试何

  • 问题内容: 这是一个按钮: 和绑定事件: 一切正常,都发现一件麻烦事,我在Chrome开发者控制台中看到两个请求: 添加/ cartManager: 添加/ cartManager / add ?: 两者的请求标头几乎相同,只是请求标头不同: 第一个是 cartManager / add?pictureId = 等,第二个是 cartManager / add /?pictureId- / add

  • 所以我不知道这是否真的描述了我的问题,但这是我能得到的最接近的吗? 我正在使用AWSAppsyncClient进行一些GraphQL突变。由于模型的性质,在更大程度上,由于我缺乏经验,我需要创建一个记录,然后创建两个依赖于第一个的记录,然后才能将它们链接到数据库中。 目前,我正在进行第一次变异,它返回所创建记录的ID。然后在查询返回的promise中创建中间记录。它基本上看起来像: 问题是Prom

  • 我想先除,然后乘,但不知道怎么做这两个想法? 它在工作,但它只是分裂的时刻。