【问题标题】:Signal emitting in Python causes exit code 139Python 中发出的信号导致退出代码 139
【发布时间】:2016-11-20 20:21:12
【问题描述】:

我正在尝试制作函数,它将 8 个值传递给使用 QtDesigner 制作的窗口应用程序中的进度条。 那就是有一个函数可以实际生成这个值。我想每秒执行一次,以获取这些值并更新显示值的进度条。

我结合了有关使用 Python 制作图形应用程序和制作具有动态更新进度条的应用程序的教程:

  1. Python Qt 开发:https://www.youtube.com/watch?v=eD91nE8q8Nk
  2. 带有线程的 PyQt 进度条:https://www.youtube.com/watch?v=ivcxZSHL7jM

问题是正确传递了值,但是在发送信号时,整个应用程序崩溃并显示消息:

进程以退出代码 139 结束(被信号 11 中断: SIGSEGV)

这是我的代码:

生成值的模块(probkowanie.py)

import time
from random import randint
import threading

def getSignals():
    time.sleep(1)
    signals = (randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), )
    return signals

这是主程序代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PySide.QtCore import *
from PySide.QtGui import *
from PyQt4 import QtCore
import sys
import time

import signalPreview # window library
import probkowanie # values generating library

class MainDialog(QDialog, signalPreview.Ui_mainDialog):

    def __init__(self, parent=None):
        super(MainDialog, self).__init__(parent)
        self.setupUi(self)
        self.threadclass = ThreadClass()
        self.threadclass.start()
        self.connect(self.threadclass, QtCore.SIGNAL('MYO_SIGNALS'), self.updateProgressBars)

    def updateProgressBars(self, signals):
        self.progressBar.setValue(signals[0])

class ThreadClass(QtCore.QThread):
    def __init__(self, parent = None):
        super(ThreadClass, self).__init__(parent)

    def run(self):
        while 1:
            signals = probkowanie.getSignals()
            self.emit(QtCore.SIGNAL('MYO_SIGNALS'), 5) # this line causes crash
            print signals # it works correctly

app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()

我认为这可能是由混合库引起的。我使用来自 PySide 的 QtCore,即使教程号 2 基于 PyQt4。我的决定是因为教程 1 是基于 PySide。 我尝试改变:

from PySide import QtCore

from PyQt4 import QtCore

然后我又收到一堆错误,我不知道该怎么办:

Traceback (most recent call last):
   File "/usr/lib/pycharm/helpers/pydev/pydevd.py", line 1580, in <module>
        globals = debugger.run(setup['file'], None, None, is_module)
   File "/usr/lib/pycharm/helpers/pydev/pydevd.py", line 964, in run
        pydev_imports.execfile(file, globals, locals)  # execute the script
   File "/mnt/Grubas/Projekty/Biomed/MYO/myo.py", line 36, in <module>
        form = MainDialog()
   File "/mnt/Grubas/Projekty/Biomed/MYO/myo.py", line 20, in __init__
        self.connect(self.threadclass, QtCore.SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
   TypeError: 'PySide.QtCore.QObject.connect' called with wrong argument types:
   PySide.QtCore.QObject.connect(ThreadClass, str, instancemethod)
   Supported signatures:
   PySide.QtCore.QObject.connect(PySide.QtCore.QObject, str, callable, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
   PySide.QtCore.QObject.connect(PySide.QtCore.QObject, PySide.QtCore.QMetaMethod, PySide.QtCore.QObject, PySide.QtCore.QMetaMethod, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
   PySide.QtCore.QObject.connect(PySide.QtCore.QObject, str, PySide.QtCore.QObject, str, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
   PySide.QtCore.QObject.connect(PySide.QtCore.QObject, str, str, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)   PySide.QtCore.QObject.connect(str, callable, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
   PySide.QtCore.QObject.connect(str, PySide.QtCore.QObject, str, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)

@编辑 提供@ekhumoro 脚本提出的更改后不会崩溃,但现在我遇到另一个错误:

QObject::connect: 不能将“object”类型的参数排队(确保 'object' 是使用 qRegisterMetaType() 注册的。)

我试图自己寻找解决方案,但没有找到我需要的确切代码。我还尝试将信号类型从(对象)转换为(元组)或(列表),但这会导致另一个错误:

TypeError: 用于调用元函数的未知类型(可能是 信号):元组

我发现的大多数解决方案都是基于 PyQT。有没有一种简单的方法可以为 PySide 重写它? 这是一个解决方案示例,它似乎是正确的,但使用 PyQT 制作: https://stackoverflow.com/a/2595607/2550466

【问题讨论】:

    标签: python multithreading pyside signals-slots qthread


    【解决方案1】:

    您认为混合 PySide 和 PyQt 会导致问题是正确的,因此您需要删除其中一个。但是,崩溃本身可能是由 PySide 中的错误引起的。这里有一个类似问题的 SO question:

    所以你还需要改变你定义和发出MYO_SIGNALS的方式。

    以下是您的脚本的固定版本(更改的行已注释):

    # from PySide.QtCore import *
    # from PySide.QtGui import *
    # from PyQt4 import QtCore
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import sys
    import time
    
    import signalPreview # window library
    import probkowanie # values generating library
    
    class MainDialog(QDialog):
    
        def __init__(self, parent=None):
            super(MainDialog, self).__init__(parent)
            self.setupUi(self)
            self.threadclass = ThreadClass()
            self.threadclass.start()
            # self.connect(self.threadclass, QtCore.SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
            self.connect(self.threadclass, SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
    
        def updateProgressBars(self, signals):
            self.progressBar.setValue(signals[0])
    
    # class ThreadClass(QtCore.QThread):
    class ThreadClass(QThread):
        def __init__(self, parent = None):
            super(ThreadClass, self).__init__(parent)
    
        def run(self):
            while 1:
                signals = probkowanie.getSignals()
                # self.emit(QtCore.SIGNAL('MYO_SIGNALS'), 5) # this line causes crash
                self.emit(SIGNAL('MYO_SIGNALS'), signals)
                print signals # it works correctly
    
    app = QApplication(sys.argv)
    form = MainDialog()
    form.show()
    app.exec_()
    

    【讨论】:

    • 由于其他错误,我已编辑我的帖子。如何对“对象”类型的参数进行排队?
    • @Asmox。我认为您必须使用旧版本的 pyside,因为它对我来说很好用。无论如何,我已经更新了我的答案,改为使用 pyqt4,这对你也应该没问题。
    猜你喜欢
    • 1970-01-01
    • 2022-07-05
    • 2021-03-07
    • 2019-08-11
    • 2018-08-31
    • 1970-01-01
    • 2021-03-14
    • 2016-02-16
    相关资源
    最近更新 更多