开发者

Using gobject.timeout_add_seconds - Segmentation Fault

I am writing a gui program that allows a user to repeatedly send a message to a phone number with a configurable delay and number of repetitions.

I used QT Designer to create a gui, and now I am trying to create the code behind it. I am trying to make the program start sending messages when the start button is pressed, but not freeze the gui.

I am trying to use gobject.timeout_add_seconds to check if new messages need to be send every 1s, but when it is causing a Segmentation Fault.

queueMessages is called whenever the button is pressed to start sending messages, and sendMessages should be run every 1s to send any needed messages.

Let me know if there is an easier way to do this (such as threading). I am open to any other ideas.

Here's the applicable code. I can include the gui code too if that would be helpful:

#!/usr/bi开发者_运维问答n/python2.5
import sys, os
import time
import gobject
from PyQt4 import QtGui,QtCore
from smsBomb import *

class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        #build parent user interface
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # Create button actions
        QtCore.QObject.connect(self.ui.btnSendMessages, QtCore.SIGNAL('clicked()'), self.queueMessages)
        # Check if we need to send any messages every 1s.
        self.maintimer = gobject.timeout_add_seconds(1, self.sendMessages)

    def queueMessages(self):
        # Queue messages!
        number = str(self.ui.txtNumber.text())
        message = str(self.ui.txtMessage.text())
        delay = int(self.ui.txtDelay.text())
        repetitions = int(self.ui.txtRepetitions.text())
        for i in range(repetitions):
            os.system('dbus-send --dest=org.QGVDial.TextServer --session --print-reply /org/QGVDial/TextServer org.QGVDial.TextServer.Text array:string:"+1' + number + '" string:"' + message + '"')
            #time.sleep(delay)

    def sendMessages(self):
        # Send Queued Messages as needed
        print "Sending queued messages..."
        return True

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())


You appear to be using a (Py)GTK timer object within a (Py)Qt application. Try replacing

    self.maintimer = gobject.timeout_add_seconds(1, self.sendMessages)

with the equivalent PyQt code

    self.maintimer = QtCore.QTimer(self);
    self.connect(self.maintimer, QtCore.SIGNAL('timeout()'), self.sendMessages)
    self.maintimer.start(1000)

I was able to reproduce the segfault using gobject.timeout_add_seconds, and it went away once I replaced the PyGTK timer with a PyQt one. I can't be sure why this happens, but this article gives a possible reason:

One caveat I found, gobject.timeout_add_seconds() seems to depend on the GTK main loop, so you cannot use it from a regular non-GTK python application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜