PyQt4 - 无法接收sender()信号/如何识别单击哪个按钮并将其与相应的进度条匹配?

时间:2013-12-23 23:28:43

标签: python pyqt4

为什么我无法接收self.sender()输出?我的观点是确定单击了哪个按钮(以启动下载功能),然后匹配下载指示器的相应进度条。

错误输出为:

Traceback (most recent call last): File "final.py", line 308, in buttonClicked buttonHandle = self.sender() AttributeError: 'Ui_MainWindow' object has no attribute 'sender'

很多。

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.setGeometry(600,300,727,455)
        MainWindow.setMinimumSize(QtCore.QSize(727, 455))
        #MainWindow.setMaximumSize(QtCore.QSize(727, 455))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 727, 23))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName(_fromUtf8("menuFile"))
        self.menuAbout = QtGui.QMenu(self.menubar)
        self.menuAbout.setObjectName(_fromUtf8("menuAbout"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.actionExit = QtGui.QAction(MainWindow)
        self.actionExit.setObjectName(_fromUtf8("actionExit"))
        self.actionPythAri = QtGui.QAction(MainWindow)
        self.actionPythAri.setObjectName(_fromUtf8("actionPythAri"))
        self.menuFile.addAction(self.actionExit)
        self.menuAbout.addAction(self.actionPythAri)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuAbout.menuAction())
        # scrollArea
        self.mainLayout = QtGui.QVBoxLayout()
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.scrollAreaLayout = QtGui.QVBoxLayout()
        #groupBoxes
        self.groupBoxes = QtGui.QWidget()
        self.groupBoxesLayout = QtGui.QVBoxLayout()
        self.groupBoxes.setLayout(self.groupBoxesLayout)
        self.groupBoxesLayout.setContentsMargins(5,5,5,5)

        for i in range(3):
            groupBox = QtGui.QGroupBox(_fromUtf8('Issue #%d' %i))
            groupLayout = QtGui.QVBoxLayout()
            #groupEllements
            self.groupBoxUpWidget = QtGui.QWidget()
            self.groupBoxUpLayout = QtGui.QHBoxLayout()
            self.groupBoxDownWidget = QtGui.QWidget()
            self.groupBoxDownLayout = QtGui.QHBoxLayout()
            self.groupBoxUpWidget.setLayout(self.groupBoxUpLayout)
            self.groupBoxDownWidget.setLayout(self.groupBoxDownLayout)
            #mainwidget-temp
            self.groupBoxMainWidget = QtGui.QWidget()
            self.groupBoxMainLayout = QtGui.QVBoxLayout()
            self.groupBoxMainWidget.setLayout(self.groupBoxMainLayout)
            # ISSUE THUMB
            self.issueThumb(self.groupBoxUpLayout)
            #SETTING MAIN LAYOUT FOR EVERY GROUPBOX
            groupBox.setLayout(groupLayout)
            # text box
            #self.txtLayout = QtGui.QHBoxLayout()
            txt = QtGui.QLabel()
            txt.setMaximumWidth(500)
            txt.setText(_fromUtf8('Issue Description'))
            #self.txtLayout.addWidget(self.txt)
            #self.txt.setLayout(self.txtLayout)
            #progressBars
            progressBar = QtGui.QProgressBar()
            progressBar.setMaximumWidth(500)
            # controls
            readBtn = QtGui.QPushButton(_fromUtf8('Read it!'),groupBox)
            readBtn.setMaximumWidth(100)
            #readBtn.setText()

            readBtn.clicked.connect(self.buttonClicked)
            # ADD EVERY WIDGET TO THE APPROPRIATE LAYOUT
            self.groupBoxUpLayout.addWidget(txt)
            self.groupBoxDownLayout.addWidget(readBtn)
            self.groupBoxDownLayout.addWidget(progressBar)
            # ADD PARENT WIDGETS TO MAIN GROUPBOX LAYOUT
               groupLayout.addWidget(self.groupBoxUpWidget)         
            groupLayout.addWidget(self.groupBoxDownWidget)          
            groupLayout.addWidget(self.groupBoxMainWidget)          


            #ADD EVERY GROUPBOX TO A MAIN LAYOUT
            self.groupBoxesLayout.addWidget(groupBox)


        #self.scrollArea.setLayout(self.scrollAreaLayout)
        self.scrollArea.setWidget(self.groupBoxes)
        self.mainLayout.addWidget(self.scrollArea)  
        self.centralwidget.setLayout(self.mainLayout)

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

        authenticatedUser = False
        authenticatedUser = self.authenticateDialog()
        print authenticatedUser
        if authenticatedUser == False:
            sys.exit(0)



    def buttonClicked(self):
        buttonHandle = self.sender()
        print buttonHandle


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui2 = Ui_MainWindow()
    ui2.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:7)

def buttonClicked(self):
    buttonHandle = self.sender()
    print buttonHandle

self here将是Ui_MainWindow,它不是QT对象,您可能想要检查MainWindow的.sender。所以将UI_MainWindow的类变量设置为MainWindow对象并检查该对象的发送者。

def setupUi(self, MainWindow):
    self.MainWindow = MainWindow
    ....

def buttonClicked(self):
    buttonHandle = self.MainWindow.sender()
    print buttonHandle