Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

python - pyQt: radioButton.isChecked() is executed twice

I have this simple window (design.py) derived from Qt designer, which consists of three radio buttons:

# -*- 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))

and I have added this code, in order to monitor which radio button is checked.

# -*- 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()    

I noticed that if radioButton1 is checked, it seems to work fine, but if radiobutton2 or radiobutton3 are checked, the check message is printed twice.

On the other hand, if I connect every signal to a different function, like this:

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'

then it works as expected.

So, I guess that the trouble occurs when I want to connect many signal to one function. Could someone explain this behavior?

Any thoughts would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The toggle() signal is emitted every time any radio button's state changes. As a result, the toggle() signal is emitted when you click on a radio button and that radio button's state changes from unchecked to checked, and if clicking on the radio button automatically unchecks another radio button, then the toggle() signal is emitted again because the other radio button's state changes from checked to unchecked.

You can see that in action by adding the following line to the end of your slot:

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

Use the clicked() signal instead--a radio button whose state was automatically changed from checked to unchecked was never clicked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...