0

I just started using PyQt and I was wondering how can I connect two windows I made on PyQt4 designer using a button press? For example when I press the button the new window pops up on my screen. The solutions I found (like the one suggested) all involve an __init__ method, however when I converted my .ui to a .py file, no __init__ function is made.

Attached are my two codes I want to connect: Code 1:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, 
_encoding)
except AttributeError:
    def _translate(context, text, disambig):
         return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        '''a bunch of initialization of widgets and buttons'''
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", 
        None))
        self.label.setText(_translate("MainWindow", "Preferences", None))
        self.newbtn.setText(_translate("MainWindow", "Add New", None))
        self.label_2.setText(_translate("MainWindow", "Current Units: US", None))
        self.pushButton.setText(_translate("MainWindow", "US", None))
        self.pushButton_2.setText(_translate("MainWindow", "Metric", None))
        self.label_5.setText(_translate("MainWindow", "List of Devices", None))
        item = self.tableWidget.verticalHeaderItem(0)
        item.setText(_translate("MainWindow", "High", None))
        item = self.tableWidget.verticalHeaderItem(1)
        item.setText(_translate("MainWindow", "Low", None))
        __sortingEnabled = self.tableWidget.isSortingEnabled()
        self.tableWidget.setSortingEnabled(False)
        item = self.tableWidget.item(0, 0)

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

Code 2: from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
         return s

 try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
     def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(402, 300)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)   
        '''a lot of initialization stuff'''

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "SN:", None))
        self.label_2.setText(_translate("Dialog", "Name:", None))
        self.label_3.setText(_translate("Dialog", "Register New Device", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
  • 2
    Possible duplicate of [PyQT: how to open new window](https://stackoverflow.com/questions/36768033/pyqt-how-to-open-new-window) – eyllanesc Jun 28 '18 at 23:23
  • I've seen that answer before, but the problem is when I converted my .ui to .py, there is no __init__ method. So I didn't know where to call the functions of the other dialog –  Jun 29 '18 at 17:04
  • @ekcal8. PyQt docs: [Using the Generated Code](http://pyqt.sourceforge.net/Docs/PyQt4/designer.html#using-the-generated-code) – ekhumoro Jun 29 '18 at 18:11
  • thank you so much! Just what I was looking for. –  Jul 02 '18 at 18:56

0 Answers0