当我试图打开自定义导入的PyQt5窗口类时,Python崩溃了

2024-03-28 10:32:33 发布

您现在位置:Python中文网/ 问答频道 /正文

说到Python,我是个新手,而PyQt5更是如此。话虽如此,但我不知道如何克服我所犯的错误,我希望有人能给我一些智慧。你知道吗

我正在尝试将外部测试PyQt5窗口脚本文件连接到我的主GUI结构。我制作了一个简单的下拉菜单GUI,其中有一个按钮,用来运行打开另一个窗口的外部脚本。我试图用以下命令执行它:test_dropButton.action.triggered.connect(testWindowButton)

我不断得到一个有趣的错误,它似乎暗示Python正在崩溃,因为我得到了这个错误:

Process finished with exit code -1073740791 (0xC0000409)根据我的研究,这意味着我可以做很多事情,从尝试调用不存在的函数,到PyQt5没有正确抛出异常。我不确定这是否是因为我的自定义窗口脚本没有一个简单地调用窗口以在屏幕上显示的函数,但是我的init方法应该在调用类时执行该操作,还是我只是胡闹而忘记了我首先需要一个构造函数?你知道吗

我还看到过将此错误解释为线程问题的解释,即尝试运行外部脚本会导致Python由于线程问题而崩溃。也许我需要多线程的外部python脚本?你知道吗

不管怎样,有人能给我解释一下上面的错误,告诉我到底发生了什么,以及为什么会崩溃吗?你知道吗

这是我的密码:

#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

下面是导入的脚本:

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_())

我希望程序打开一个带有下拉菜单的主窗口,其中一个菜单标记为“test”,其中一个按钮名为“test”,它运行一个打开另一个窗口的导入脚本。你知道吗


Tags: fromtestimportself脚本init错误sys
1条回答
网友
1楼 · 发布于 2024-03-28 10:32:33

试试看:

主.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_())  

培训helloworld.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

相关问题 更多 >