m.akbari
m.akbari

Reputation: 592

Connecting several QT windows with a QPushButton

I am working on a project and I want to develop GUI for that. I have created 4 different UI files with Qt Designer and converted them into python files with PySide (pyside-uic). Now, I want to create a main python file to connect these windows. My software's structure is as follows:

1 - Welcome window: Pressing push button to load main window and close the Welcome window 2 - Main window: it has 2 buttons to open my third and fourth windows.

I have read this post and this one post but I could not get it done (I am newbie).

This is my code for the Welcome window:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'welcome_window_gui.ui'
#
# Created: Tue Mar 29 16:49:21 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.4

#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_welcome_window(object):
    def setupUi(self, welcome_window):
        welcome_window.setObjectName("welcome_window")
        welcome_window.resize(474, 300)
        self.verticalLayout_2 = QtGui.QVBoxLayout(welcome_window)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.welcomeLBL = QtGui.QLabel(welcome_window)
        self.welcomeLBL.setAlignment(QtCore.Qt.AlignCenter)
        self.welcomeLBL.setObjectName("welcomeLBL")
        self.verticalLayout_2.addWidget(self.welcomeLBL)
        self.welcomeBTN = QtGui.QPushButton(welcome_window)
        self.welcomeBTN.setObjectName("welcomeBTN")
        self.verticalLayout_2.addWidget(self.welcomeBTN)

        self.retranslateUi(welcome_window)
        QtCore.QObject.connect(self.welcomeBTN, QtCore.SIGNAL("clicked()"), welcome_window.close)
        QtCore.QMetaObject.connectSlotsByName(welcome_window)

    def retranslateUi(self, welcome_window):
        welcome_window.setWindowTitle(QtGui.QApplication.translate("welcome_window", "Welcome", None, QtGui.QApplication.UnicodeUTF8))
        self.welcomeLBL.setText(QtGui.QApplication.translate("welcome_window", "Welcome TO the Software", None, QtGui.QApplication.UnicodeUTF8))
        self.welcomeBTN.setText(QtGui.QApplication.translate("welcome_window", "Enter to Software", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    welcome_window = QtGui.QWidget()
    ui = Ui_welcome_window()
    ui.setupUi(welcome_window)
    welcome_window.show()
    sys.exit(app.exec_())

and this is for my main window:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main_window_gui.ui'
#
# Created: Tue Mar 29 15:56:57 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_main_window(object):
    def setupUi(self, main_window):
        main_window.setObjectName("main_window")
        main_window.resize(643, 439)
        self.gridLayout_2 = QtGui.QGridLayout(main_window)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setContentsMargins(0, -1, -1, -1)
        self.gridLayout.setObjectName("gridLayout")
        self.TabWidget = QtGui.QTabWidget(main_window)
        self.TabWidget.setTabPosition(QtGui.QTabWidget.North)
        self.TabWidget.setIconSize(QtCore.QSize(16, 16))
        self.TabWidget.setObjectName("TabWidget")
        self.tab = QtGui.QWidget()
        self.tab.setObjectName("tab")
        self.gridLayoutWidget = QtGui.QWidget(self.tab)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 531, 331))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout_3 = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.TabWidget.addTab(self.tab, "")
        self.tab_2 = QtGui.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.TabWidget.addTab(self.tab_2, "")
        self.gridLayout.addWidget(self.TabWidget, 0, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.LEDbtn = QtGui.QPushButton(main_window)
        self.LEDbtn.setObjectName("LEDbtn")
        self.gridLayout_2.addWidget(self.LEDbtn, 2, 3, 1, 1)
        self.calenderBTN = QtGui.QPushButton(main_window)
        self.calenderBTN.setObjectName("calenderBTN")
        self.gridLayout_2.addWidget(self.calenderBTN, 1, 3, 1, 1)

        self.retranslateUi(main_window)
        self.TabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(main_window)

    def retranslateUi(self, main_window):
        main_window.setWindowTitle(QtGui.QApplication.translate("main_window", "Software", None, QtGui.QApplication.UnicodeUTF8))
        self.TabWidget.setTabText(self.TabWidget.indexOf(self.tab), QtGui.QApplication.translate("main_window", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
        self.TabWidget.setTabText(self.TabWidget.indexOf(self.tab_2), QtGui.QApplication.translate("main_window", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))
        self.LEDbtn.setText(QtGui.QApplication.translate("main_window", "LED", None, QtGui.QApplication.UnicodeUTF8))
        self.calenderBTN.setText(QtGui.QApplication.translate("main_window", "Calendar", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    main_window = QtGui.QWidget()
    ui = Ui_main_window()
    ui.setupUi(main_window)
    main_window.show()
    sys.exit(app.exec_())

Is there a way to call these classes in a Main Python code?

Thanks.

Upvotes: 0

Views: 75

Answers (1)

MrP01
MrP01

Reputation: 1544

You can simply import them in your main file:

from first_file import Ui_welcome_window
from second_file import Ui_main_window

And then you can use them from there, as you would normally (Note, that the if __name__ == "__main__" part in the 2 files is not executed)

To make a button:

#create the 2 windows
main_window = QtGui.QWidget()
ui = Ui_main_window()
ui.setupUi(main_window)

welcome_window = QtGui.QWidget()
ui2 = Ui_welcome_window()
ui2.setupUi(welcome_window)

ui2.welcomeBTN.clicked.connect(main_window.show)

When you click the Button on the welcome_window, the main_window should show(). Do the same with the 3rd and 4th window!

Greetings, MrP01

Upvotes: 1

Related Questions