Frank
Frank

Reputation: 469

pyqt5, Receiving AttributeError: 'QMainWindow' object has no attribute 'browseSlot'

I'm learning pyqt5, and specifically how to use it with the QT Designer. I'm sort of following the turorial HERE. However in this tutorial they are converting the XML interface to Python code with pyuic5, while I'm trying to import it dynamically with uic.loadUi("myui.ui"). In the tutorial we define a slot with the signals and slot editor named " browseSlot". When I try to run/compile, at the line

dlg = uic.loadUi("myui.ui")

I get the error:

AttributeError: 'QMainWindow' object has no attribute 'browseSlot'

I think what's going on is that QT Designer connects a signal to the slot 'browseSlot' but because a 'browseSlot' method isn't defined in the myui.ui, the error is thrown, because there is no way for the interpreter to know I'm referring to a method that is outside the UI interface file. (In this case, in the module that loads the interface). As far as I can tell QT Designer only lets me connect signals to slots, not define a whole new one. I think that way this is handled in other frameworks is that there will be an abstract method that needs over riding. So what can I do in this situation to make it work?

from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import QObject, pyqtSlot
import sys

app = QtWidgets.QApplication([])
dlg = uic.loadUi("myui.ui")

@pyqtSlot
def returnPressedSlot():
    pass
@pyqtSlot
def writeDocSlot():
    pass
@pyQt
def browseSlot():
    pass

dlg.show()
sys.exit(app.exec())

Upvotes: 1

Views: 5675

Answers (2)

Abayomi Olowu
Abayomi Olowu

Reputation: 349

try this out

from PyQt5 import QtWidgets, uic

app = QtWidgets.QApplication([])

form = uic.loadUi("login.ui")

form2.show()

app.exec()

the above python code should display your gui app properly as long as you have install PyQt5 and PyQt5-tools,if you haven't then open CMD and typeenter code here "pip install PyQt5" and click enter.once installation is done type "pip install PyQt5-tools" then you are good to go

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 243973

The slots belong to the class that is used returns loadUi(), they are not any functions since they do not magically not connect them, if you want to use loadUi() and implement these methods you must inherit from the class corresponding to the template that you used, in the example of the link Main Window was used so it must be inherited from QMainWindow:

from PyQt5 import QtCore, QtGui, QtWidgets, uic


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi("mainwindow.ui", self)

    @QtCore.pyqtSlot()
    def returnPressedSlot():
        pass

    @QtCore.pyqtSlot()
    def writeDocSlot():
        pass

    @QtCore.pyqtSlot()
    def browseSlot():
        pass


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions