-2

I followed a GUI course but the exact code does not work for me. I was trying to be guided to the next page when I clicked the button. But every time I click it, Python stops working with:

Process finished with exit code -1073740791 (0xC0000409)

Here's the code:

import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        loadUi("screen1.ui", self)
        self.button.clicked.connect(self.gotoScreen2)

    def gotoScreen2(self):
        widget.setCurrentIndex(widget.currentWidget()+1)

class Screen2(QMainWindow):
    def __init__(self):
        super(Screen2, self).__init__()
        loadUi("screen2.ui",self)
        self.button2.clicked.connect(self.gotoScreen1)

    def gotoScreen1(self):
        widget.setCurrentIndex(widget.currentWidget()+1)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = QtWidgets.QStackedWidget()

    mainwindow = MainWindow()
    widget.addWidget(mainwindow)

    screen2 = Screen2()
    widget.addWidget(screen2)

    widget.setFixedWidth(600)
    widget.setFixedWidth(800)
    widget.show()

try:
    sys.exit(app.exec())
except:
    print("Exiting..")

There may be some errors in code. It's my first time working on a GUI.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Could you run the script from terminal and update your post with the full trace of the error (no screenshot, copy paste and format). Also read https://stackoverflow.com/questions/46710299/why-does-pyqt-crashes-without-information-exit-code-0xc0000409 – jlandercy Feb 02 '23 at 11:42
  • When I ran it from terminal I had an error: ' is not recognized as an internal or external command, operable program or batch file.' – aleksmadic Feb 02 '23 at 14:32
  • Well this does not mean you executed it, it means terminal does not understand what you asked for. What happens when you execute `python script.py` (replace by the real name of your file). Update your post with the error trace (not in comment). – jlandercy Feb 02 '23 at 17:13
  • Typo: [`currentWidget()`](https://doc.qt.io/qt-5/qstackedwidget.html#currentWidget) returns a *widget*, which obviously cannot be added to a number. You should use [`currentIndex()`](https://doc.qt.io/qt-5/qstackedwidget.html#currentIndex-prop). Also, it seems you're following an infamous youtube tutorial (by a certain Hala), and if that's the case I strongly advise you to completely disregard it, as it provides ***a lot*** of terrible suggestions and bad practices. Look [here](https://wiki.python.org/moin/PyQt/Tutorials) for some *actually valid* tutorials. – musicamante Feb 02 '23 at 19:15

2 Answers2

0

I have changed the

widget.setCurrentIndex(widget.currentWidget()+1)

to

widget.setCurrentIndex(1)

and it solved my problem. Now I can switch between pages without any crashes.

-1

You have to install a module named 'PyQt5'

  • I have installed it from terminal. I used the pip install pyqt5 code – aleksmadic Feb 02 '23 at 12:04
  • @AyushyaSingh Please read more carefully: the OP says that the program crashes when they click the button, meaning that, until then, the program works. If they didn't have PyQt installed, it wouldn't have worked in the first place. – musicamante Feb 03 '23 at 02:56