python-使用gobject.timeout_add_seconds-分段错误
作者:互联网
我正在编写一个gui程序,该程序允许用户以可配置的延迟和重复次数将消息重复发送到电话号码.
我使用QT Designer创建gui,现在我试图创建它的代码.我试图使程序在按下开始按钮时开始发送消息,但不冻结gui.
我正在尝试使用gobject.timeout_add_seconds来检查是否需要每隔1秒发送一次新消息,但是这会导致分段错误.
每当按下按钮以开始发送消息时,都会调用queueMessages,并且sendMessages应该每1秒运行一次以发送任何需要的消息.
让我知道是否有更简单的方法(例如线程).我愿意接受其他任何想法.
这是适用的代码.如果有帮助,我也可以包含gui代码:
#!/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_())
解决方法:
您似乎在(Py)Qt应用程序中使用了(Py)GTK计时器对象.尝试更换
self.maintimer = gobject.timeout_add_seconds(1, self.sendMessages)
具有等效的PyQt代码
self.maintimer = QtCore.QTimer(self);
self.connect(self.maintimer, QtCore.SIGNAL('timeout()'), self.sendMessages)
self.maintimer.start(1000)
我能够使用gobject.timeout_add_seconds来复制段错误,而当我用PyQt替换了PyGTK计时器后,它就消失了.我不确定为什么会发生这种情况,但是this article给出了一个可能的原因:
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.
标签:gobject,python 来源: https://codeday.me/bug/20191102/1995209.html