0

I created a GUI using Qt designer, and converted it into .py file. I used to write the functions directly in the GUI .py file, but I've been told to separate the code into two files: one with only the GUI commands as been converted from Qt designer, and Main - that would work all of the buttons and widgets of the GUI.

Now, I've managed to import the GUI from different file, and when I run the script the GUI does come up, but when I try to write functions I seem to have troubles.

This is my main:

from PyQt5 import QtWidgets
from mainwin import Ui_MainWindow
import sys


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
       super(ApplicationWindow, self).__init__()

       self.ui = Ui_MainWindow()
       self.ui.setupUi(self)   

       self.exitbtn.clicked.connect(self.exitclicked) ***this is the connection and function that I'm trying to make 'em work***
       def exitclicked(self,Dialog):
          sys.exit()

def main():
   import sys
   app = QtWidgets.QApplication(sys.argv)
   application = ApplicationWindow()
   application.show()
   sys.exit(app.exec_())

if __name__ == "__main__":
   main()

And I don't know if it's relevant, but this is my GUI converted from Qt designer.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(895, 422)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
    self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
    self.txtoutput.setObjectName("txtoutput")
    self.comboBox = QtWidgets.QComboBox(self.centralwidget)
    self.comboBox.setGeometry(QtCore.QRect(50, 90, 69, 22))
    self.comboBox.setObjectName("comboBox")
    self.lbl = QtWidgets.QLabel(self.centralwidget)
    self.lbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
    self.lbl.setObjectName("lbl")
    self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
    self.lcdNumber.setGeometry(QtCore.QRect(340, 170, 151, 23))
    self.lcdNumber.setObjectName("lcdNumber")
    self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
    self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
    self.getserialbtn.setObjectName("getserialbtn")
    self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
    self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
    self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
    self.runbtn = QtWidgets.QPushButton(self.centralwidget)
    self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
    font = QtGui.QFont()
    font.setPointSize(28)
    font.setBold(True)
    font.setWeight(75)
    self.runbtn.setFont(font)
    self.runbtn.setCheckable(False)
    self.runbtn.setObjectName("runbtn")
    self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
    self.textEdit.setGeometry(QtCore.QRect(50, 50, 151, 31))
    self.textEdit.setObjectName("textEdit")
    self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
    self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
    font = QtGui.QFont()
    font.setPointSize(28)
    font.setBold(True)
    font.setWeight(75)
    self.stopbtn.setFont(font)
    self.stopbtn.setObjectName("stopbtn")
    self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
    self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
    self.exitbtn.setObjectName("exitbtn")
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.lbl.setText(_translate("MainWindow", "Enter serial #"))
    self.getserialbtn.setText(_translate("MainWindow", "Enter"))
    self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
    self.runbtn.setText(_translate("MainWindow", "Run"))
    self.stopbtn.setText(_translate("MainWindow", "Stop"))
    self.exitbtn.setText(_translate("MainWindow", "Exit"))

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

The error that I get when I try to run it is:

AttributeError: 'ApplicationWindow' object has no attribute 'exitbtn'

Would love if someone can write the right syntax to be able to implement functions through main.

Thank you so much :D

2
  • change self.exitbtn.clicked.connect(self.exitclicked) to self.ui.exitbtn.clicked.connect(self.exitclicked) and def exitclicked(self, Dialog): to def exitclicked(self): Commented Dec 15, 2019 at 15:38
  • Hey Nick! I changed my main as you said, but I still get the same error.. Do you have any more ideas how I can make it work? Commented Dec 15, 2019 at 15:55

1 Answer 1

1

Try it:

main.py

from PyQt5 import QtWidgets
from mainwin import Ui_MainWindow


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
       super(ApplicationWindow, self).__init__()

       self.ui = Ui_MainWindow()
       self.ui.setupUi(self)   

#       self.exitbtn.clicked.connect(self.exitclicked) 
       self.ui.exitbtn.clicked.connect(self.exitclicked)       # +

    def exitclicked(self):                                     # + 
        self.close()                                           # +

def main():
   import sys
   app = QtWidgets.QApplication(sys.argv)
   application = ApplicationWindow()
   application.show()
   sys.exit(app.exec_())

if __name__ == "__main__":
   main()

mainwin.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(895, 422)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
        self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
        self.txtoutput.setObjectName("txtoutput")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(50, 90, 69, 22))
        self.comboBox.setObjectName("comboBox")
        self.lbl = QtWidgets.QLabel(self.centralwidget)
        self.lbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
        self.lbl.setObjectName("lbl")
        self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdNumber.setGeometry(QtCore.QRect(340, 170, 151, 23))
        self.lcdNumber.setObjectName("lcdNumber")
        self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
        self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
        self.getserialbtn.setObjectName("getserialbtn")
        self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
        self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
        self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
        self.runbtn = QtWidgets.QPushButton(self.centralwidget)
        self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.runbtn.setFont(font)
        self.runbtn.setCheckable(False)
        self.runbtn.setObjectName("runbtn")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(50, 50, 151, 31))
        self.textEdit.setObjectName("textEdit")
        self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
        self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.stopbtn.setFont(font)
        self.stopbtn.setObjectName("stopbtn")
        self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
        self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
        self.exitbtn.setObjectName("exitbtn")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.lbl.setText(_translate("MainWindow", "Enter serial #"))
        self.getserialbtn.setText(_translate("MainWindow", "Enter"))
        self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
        self.runbtn.setText(_translate("MainWindow", "Run"))
        self.stopbtn.setText(_translate("MainWindow", "Stop"))
        self.exitbtn.setText(_translate("MainWindow", "Exit"))

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.