Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt4 - can't receive sender() signal / how to indentify which button is clicked and match it with the appropriate progressbar?

Tags:

python

pyqt4

Why i can't receive self.sender() output? My point is to identify which button is clicked (to start a download function) and then match the corresponding progress bar for a downloading indicator.

The error output is:

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

thnx a lot.

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_())
like image 964
specktator Avatar asked Dec 23 '13 23:12

specktator


1 Answers

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

self here would be the Ui_MainWindow which is not a QT object, you probably want to check .sender of MainWindow. so set a class variable of UI_MainWindow to the MainWindow object and check sender of that object.

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

def buttonClicked(self):
    buttonHandle = self.MainWindow.sender()
    print buttonHandle
like image 102
M4rtini Avatar answered Sep 28 '22 09:09

M4rtini