I am a total beginner with PyQt5, please excuse my ignorance
I am trying to connect a push button with a function that I defined
import sys
from PyQt5 import QtGui, QtWidgets,uic
def PrintSomething ():
print("Hello world")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = uic.loadUi("Auto.ui")
window.show()
sys.exit(app.exec_())
In order to do the above I added a slot to the MainWindow using QtDesigner ..as seen in the picture and then I connected the Pressed signal for the button with the slot of the main window
When I try to run the app I am getting this error:
AttributeError: 'QMainWindow' object has no attribute 'PrintSomething'
What am I doin wrong here ?
Update:
I have also used this code
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from Auto import Ui_MainWindow
class MainW (QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == '__main__':
#topology=topo.LoadTopology()
app = QApplication(sys.argv)
myapp = MainW()
myapp.show()
sys.exit(app.exec_())
Where the Auto module was generated via pyuic5 and the function PrintSomething() was added. this also results in an error
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(667, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(300, 380, 95, 29))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 667, 23))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuExit = QtWidgets.QMenu(self.menubar)
self.menuExit.setObjectName("menuExit")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionClose)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuExit.menuAction())
self.retranslateUi(MainWindow)
self.pushButton.pressed.connect(MainWindow.PrintSomething())
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuExit.setTitle(_translate("MainWindow", "Exit"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionClose.setText(_translate("MainWindow", "Close"))
def PrintSomething ():
print("Hello world")