M
M
Mswork62020-06-14 14:32:06
Python
Mswork6, 2020-06-14 14:32:06

How to implement transition from one window to another and back in PyQt5?

Hello. I want to create an interactive tutorial application in PyQt5. I need to navigate from the table of contents window to a specific unit and back. I started learning PyQt5 recently, so I'm currently practicing. I started to implement my plans, created a window - a table of contents, and a window - a test. I can go from the table of contents window to the test window, but I can’t go back. If I use the same method as in the transition from the table of contents to the test, I get an Import Error. I don't know how to do it otherwise. Can you please tell me how this can be implemented? I design windows in QtDesigner (Part of the code of the second file was not included, I had to remove the ending)

ContentsWindow - the table of contents window

from PyQt5 import QtCore, QtGui, QtWidgets
from TaskWindow1 import Ui_TaskWindow_1


class Ui_ContentsMenu(object):

    def gototask1(self):
        self.window1 = QtWidgets.QMainWindow()
        self.ui1 = Ui_TaskWindow_1()
        self.ui1.setupUi(self.window1)
        ContentsMenu.hide()
        self.window1.show()



    def setupUi(self, ContentsMenu):
        ContentsMenu.setObjectName("ContentsMenu")
        ContentsMenu.resize(649, 481)
        self.centralwidget = QtWidgets.QWidget(ContentsMenu)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(30, 120, 301, 261))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton_3 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.gridLayout.addWidget(self.pushButton_3, 0, 0, 1, 1)
        self.pushButton_2 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 2, 0, 1, 1)
        self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 1, 0, 1, 1)
        self.pushButton_4 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_4.setObjectName("pushButton_4")
        self.gridLayout.addWidget(self.pushButton_4, 0, 1, 1, 1)
        self.pushButton_5 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_5.setObjectName("pushButton_5")
        self.gridLayout.addWidget(self.pushButton_5, 1, 1, 1, 1)
        self.pushButton_6 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_6.setObjectName("pushButton_6")
        self.gridLayout.addWidget(self.pushButton_6, 2, 1, 1, 1)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200, 20, 261, 71))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(410, 180, 171, 71))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        self.label_2.setFont(font)
        self.label_2.setText("")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        ContentsMenu.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(ContentsMenu)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 649, 21))
        self.menubar.setObjectName("menubar")
        ContentsMenu.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(ContentsMenu)
        self.statusbar.setObjectName("statusbar")
        ContentsMenu.setStatusBar(self.statusbar)
        self.retranslateUi(ContentsMenu)
        QtCore.QMetaObject.connectSlotsByName(ContentsMenu)


        self.pushButton_3.clicked.connect(self.gototask1)

    def retranslateUi(self, ContentsMenu):
        _translate = QtCore.QCoreApplication.translate
        ContentsMenu.setWindowTitle(_translate("ContentsMenu", "MainWindow"))
        self.pushButton_3.setText(_translate("ContentsMenu", "Task 1"))
        self.pushButton_2.setText(_translate("ContentsMenu", "Task 3"))
        self.pushButton.setText(_translate("ContentsMenu", "Task 2"))
        self.pushButton_4.setText(_translate("ContentsMenu", "Task 4"))
        self.pushButton_5.setText(_translate("ContentsMenu", "Task 5"))
        self.pushButton_6.setText(_translate("ContentsMenu", "Task 6"))
        self.label.setText(_translate("ContentsMenu", "Contents Table"))


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


TaskWindow1 - window with the test
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_TaskWindow_1(object):

    def setupUi(self, TaskWindow_1):
        TaskWindow_1.setObjectName("TaskWindow_1")
        TaskWindow_1.resize(689, 456)
        self.centralwidget = QtWidgets.QWidget(TaskWindow_1)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 351, 71))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.VideoButton = QtWidgets.QPushButton(self.centralwidget)
        self.VideoButton.setGeometry(QtCore.QRect(380, 50, 141, 31))
        font = QtGui.QFont()
        font.setUnderline(True)
        self.VideoButton.setFont(font)
        self.VideoButton.setObjectName("VideoButton")
        self.QuestionLabel_1 = QtWidgets.QLabel(self.centralwidget)
        self.QuestionLabel_1.setGeometry(QtCore.QRect(30, 120, 191, 41))
        self.QuestionLabel_1.setObjectName("QuestionLabel_1")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(20, 160, 131, 131))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.AnswerButton_1_2 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.AnswerButton_1_2.setObjectName("AnswerButton_1_2")
        self.gridLayout.addWidget(self.AnswerButton_1_2, 1, 0, 1, 1)
        self.AnswerButton_1_3 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.AnswerButton_1_3.setObjectName("AnswerButton_1_3")
        self.gridLayout.addWidget(self.AnswerButton_1_3, 2, 0, 1, 1)
        self.AnswerButton_1_1 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.AnswerButton_1_1.setObjectName("AnswerButton_1_1")
        self.gridLayout.addWidget(self.AnswerButton_1_1, 0, 0, 1, 1)
        self.AnswerLabel_1 = QtWidgets.QLabel(self.centralwidget)
        self.AnswerLabel_1.setGeometry(QtCore.QRect(190, 190, 121, 41))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question