2016-04-23 59 views
0

我已經這個簡單的窗口(design.py)從Qt設計衍生,它由三個單選按鈕:PyQt的:radioButton.isChecked()被執行兩次

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

那麼它將按預期工作。

所以,我想當我想將許多信號連接到一個功能時會發生問題。有人可以解釋這種行爲嗎?

任何想法將不勝感激。

回答

4

toggle()信號每次發出任意單選按鈕的狀態發生變化。因此,當您單擊一個單選按鈕並且該單選按鈕的狀態從未選中更改爲已選中時,將發出toggle()信號,並且如果單擊單選按鈕會自動取消選中另一個單選按鈕,則會再次發射toggle()信號,因爲另一個單選按鈕單選按鈕的狀態從勾選變爲未勾選。

你可以看到,在行動中加入下面一行到你的槽的末端:

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

使用clicked()信號,而不是 - 一個單選按鈕,其狀態已自動從檢查未經檢查的改變從來沒有點擊。

+0

它工作正常。謝謝。 – user3060854