Python crashes when I try to open a custom imported PyQt5 window class

0

I am a bit of a rookie when it comes to Python, and even more so with PyQt5. With that said, I am not sure how to move forward with the error I am getting, and I am hoping someone can give me some wisdom here.

I am trying to connect an external test PyQt5 window script file to my main GUI structure. I have made a simple drop down menu GUI with a button that is supposed to run the external script that will open another window. I am trying to execute it with this command: test_dropButton.action.triggered.connect(testWindowButton)

I keep getting an interesting error that seems to hint that Python is crashing as I get this error:

Process finished with exit code -1073740791 (0xC0000409) and from my research this means that I could be doing a number of things from trying to call a function that does not exist, to PyQt5 not throwing an exception correctly. I am unsure if it is because my custom window script does not have a function that simply calls the window to show on the screen, however my init method should be doing that when the class is called, or did I just goof and forget I need a constructor first?

I have also seen explanations of this error as a threading issue in which trying to run an external script can cause Python to crash due to threading issues. Perhaps I need to multi thread the external python script?

Either way, can someone explain to me the above error, and tell me exactly what is going on, and why it might be crashing?

Here is my code:

#This is a snippet of my main GUI structure

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
import TrainHelloWorld as hi

 mainMenu = self.menuBar()
 trainMenu = mainMenu.addMenu('Test')

testWindowButton = hi.Greeting()

    test_dropButton = QAction('test', self)
    test_dropButton.setShortcut('Ctrl+T')
    test_dropButton.setStatusTip('Test the button')
    test_dropButton.action.triggered.connect(testWindowButton.show())
    trainMenu.addAction(test_dropButton)  # add test button to dropdown menu

Here is the imported script:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
import sys


class Greeting(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Dummy Hello'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 400
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        greetingLabel = QLabel()

        greetingLabel.setText("You called a Python function in a QAction menu, YOU DID IT!")

        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Greeting()
    sys.exit(app.exec_())

I expect the program to open a single main window with a drop down menu with a single menu labeled "test" with a button names "test" that runs an imported script that opens another window.

python
exception-handling
pyqt5
python-multithreading
asked on Stack Overflow Feb 3, 2019 by Zthom

1 Answer

0

Try it:

main.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
import TrainHelloWorld as hi


class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('MyMainWindow')

        mainMenu  = self.menuBar()
        trainMenu = mainMenu.addMenu('Test')

        self.testWindowButton = hi.Greeting() 

        test_dropButton = QAction('test', self)
        test_dropButton.setShortcut('Ctrl+T')
        test_dropButton.setStatusTip('Test the button')

        # test_dropButton.action.triggered.connect(testWindowButton.show())
        #                 ------                                        --
        test_dropButton.triggered.connect(self.testWindowButton.show)  # - `.action`, - `()`
        trainMenu.addAction(test_dropButton)  # add test button to dropdown menu   


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyMainWindow()
    ex.show()
    sys.exit(app.exec_())  

TrainHelloWorld.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import QIcon

class Greeting(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Dummy Hello'
        self.left   = 520
        self.top    = 280
        self.width  = 640
        self.height = 400
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        greetingLabel = QLabel(self)                    # + self
        greetingLabel.setGeometry(170, 200, 300, 50)    # +++
        greetingLabel.setText("You called a Python function in a QAction menu, YOU DID IT!")
# ---   self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Greeting()
    ex.show()
    sys.exit(app.exec_())

enter image description here

answered on Stack Overflow Feb 3, 2019 by S. Nick

User contributions licensed under CC BY-SA 3.0