Using gobject.timeout_add_seconds - Segmentation Error

I am writing a gui program that allows a user to resend a message to a phone number with a custom delay and the number of repetitions.

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

I am trying to use gobject.timeout_add_seconds to check if new messages should be sent every 1 s, but when a segmentation error occurs.

queueMessages is called whenever a button is pressed to start sending messages, and sendMessages should be run every 1 second to send any necessary messages.

Let me know if there is an easier way to do this (e.g. threads). I am open to any other ideas.

Here is the applicable code. I can also include gui code if this were useful:

#!/usr/bin/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_())
+3
source share
1 answer

It seems you are using the (Py) GTK timer object in the (Py) Qt application. Try replacing

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

with 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 play segfault with gobject.timeout_add_secondsand it was gone as soon as I replaced the PyGTK timer with PyQt. I can’t be sure why this is happening, but this article gives a possible reason:

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

+3
source

Source: https://habr.com/ru/post/1790320/


All Articles