我有一个示例pyside演示,我创建它以查看webkit浏览器与python的通信... 我在webkit中有两个按钮
按钮1 - 点击时,它会睡眠10秒钟,然后打印一条消息
button2 - 点击时会立即打印一条消息。
当我点击按钮1时,整个应用程序冻结并等待python完成睡眠,这意味着我无法点击按钮2来执行其他操作。如何在函数调用之间实现异步方法?
我的python代码在
之下import sys,json
from time import sleep
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebView, QWebSettings
from PySide.QtNetwork import QNetworkRequest
from PySide.QtCore import QObject, Slot, Signal
html_str="""<!doctype>
<html>
<body>hello world
<button id="button" >button1</button>
<button id="button2" >button2</button>
</body>
</html>
<script type="text/javascript">
document.getElementById("button").onclick=function(){
object.reply(" hello ");
}
document.getElementById("button2").onclick=function(){
object.reply2(" hello ");
}
function data_from_js(msg){
var tag=document.createElement('div');
tag.innerHTML="message from python";
document.body.appendChild(tag);
alert(msg['name']);
}
</script>
<style>
body{
border:solid black 1px;
}
</style>
</doctype>"""
class Qbutton(QObject):
from time import sleep
def __init__(self):
super(Qbutton,self).__init__()
@Slot(str)
def reply(self,recd):
#r=QMessageBox.information(self,"Info",msg)
msgBox = QMessageBox()
sleep(10)
msgBox.setText("python just said"+recd)
msgBox.exec_()
return "I am recieving pythonic data"
#r=QMessageBox.question(self,title,recd,QMessageBox.Yes | QMessageBox.No)
@Slot(str)
def reply2(self,recd):
msgBox = QMessageBox()
msgBox.setText("python just said"+recd+ " another time")
msgBox.exec_()
return "I am recieving pythonic data"
@Slot(str)
def send_tojs(self):
pass
class adstar_gui(QWidget):
def __init__(self):
super(adstar_gui,self).__init__()
self.setWindowTitle("Adstar Wordlist Generator")
self.setMaximumWidth(5000)
self.setMaximumHeight(5000)
self.setMinimumWidth(500)
self.setMinimumHeight(500)
self.show()
print "Sample window"
def closeEvent(self,event):
self.closeEvent()
if __name__=="__main__":
Qapp=QApplication(sys.argv)
t=QWebView()
t.setHtml(html_str)
button=Qbutton()
t.page().mainFrame().addToJavaScriptWindowObject("object",button)
t.show()
#t.page().mainFrame().evaluateJavaScript("data_from_js(%s);" % (json.dumps({'name':"My name is Junior"}) ))
QCoreApplication.processEvents()
#sys.exit(Qapp.exec_())
Qapp.exec_()
问题
如何点击webkit中的button 1
并让python在单击按钮1时在后台执行某些操作? (这样button 2
函数不需要等待按钮1功能完成)
请使用此演示并对其进行改进...非常感谢
答案 0 :(得分:2)
这里有几个问题。首先,值得指出为什么当你点击button1时应用程序冻结:点击导致Qt调用事件处理程序reply
,并且Qt无法处理另一个事件,直到此处理程序返回(根据我的经验,所有窗口系统以这种方式工作)。因此,如果您在事件处理程序中放置任何长时间运行的例程,您的应用程序将冻结,直到例程结束。每当事件处理程序花费的时间超过0.05秒时,用户就会注意到。
正如titusjan在他的回答中指出的那样,在一段时间间隔之后让Qt执行一个函数非常容易。但我认为你的问题不是如何处理简单的时间延迟,而是如何处理长时间运行的进程。在我的示例代码中,我用一个计算十秒一秒延迟的循环替换了你的十秒延迟,我认为这是你想要实现的更好的模型。
解决方案是在另一个线程中执行漫长的过程。您有两个选择:QThreads,它是Qt环境的一部分,以及Python线程。它们都可以工作,但我总是尽可能使用Python线程。它们的记录更好,重量更轻一些。将线程指定为守护进程的能力有时会使应用程序关闭变得更加简单。此外,将多线程程序转换为使用多进程的程序更容易。我在下面的示例代码中使用了Python线程。
然后出现问题,应用程序如何知道辅助线程何时完成?为此,您必须创建自定义Qt信号。您的辅助线程在完成工作后会发出此信号,并且主应用程序连接一个Slot以在发生这种情况时执行某些操作。如果您要创建自定义Qt信号,则必须在QObject的子类中声明它,就像我在示例中所做的那样。
毋庸置疑,必须处理所有标准的多线程问题。
import sys
import json
import threading
from time import sleep
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebView, QWebSettings
from PySide.QtNetwork import QNetworkRequest
from PySide.QtCore import QObject, Slot, Signal
html_str="""<!doctype>
<html>
<body>hello world
<button id="button" >button1</button>
<button id="button2" >button2</button>
</body>
</html>
<script type="text/javascript">
document.getElementById("button").onclick=function(){
object.reply(" hello ");
}
document.getElementById("button2").onclick=function(){
object.reply2(" hello ");
}
function data_from_js(msg){
var tag=document.createElement('div');
tag.innerHTML="message from python";
document.body.appendChild(tag);
alert(msg['name']);
}
</script>
<style>
body{
border:solid black 1px;
}
</style>
</doctype>"""
class Qbutton(QObject):
def __init__(self):
super(Qbutton,self).__init__()
self.long_thread = LongPythonThread()
self.long_thread.thread_finished.connect(self.reply2_finished)
@Slot(str)
def reply(self,recd):
print("reply")
t = threading.Thread(target=self.long_thread.long_thread, args=(recd,))
t.daemon = True
t.start()
@Slot(str)
def reply2(self,recd):
print("reply2")
msgBox = QMessageBox()
msgBox.setText("python just said"+recd)
msgBox.exec_()
return "I am receiving pythonic data"
@Slot(str)
def reply2_finished(self, recd):
print("reply2 finished")
msgBox = QMessageBox()
msgBox.setText("python just said"+recd+ " another time")
msgBox.exec_()
@Slot(str)
def send_tojs(self):
pass
class LongPythonThread(QObject):
thread_finished = Signal(str)
def __init__(self):
super(LongPythonThread,self).__init__()
def long_thread(self, recd):
for n in range(10):
sleep(1.0)
print("Delayed for {:d}s".format(n+1))
self.thread_finished.emit(recd)
if __name__=="__main__":
Qapp=QApplication(sys.argv)
t=QWebView()
t.setHtml(html_str)
button=Qbutton()
t.page().mainFrame().addToJavaScriptWindowObject("object",button)
t.show()
#t.page().mainFrame().evaluateJavaScript("data_from_js(%s);" % (json.dumps({'name':"My name is Junior"}) ))
QCoreApplication.processEvents()
#sys.exit(Qapp.exec_())
Qapp.exec_()
答案 1 :(得分:1)
使用QTimer
在一段时间后执行信号。像这样:
import sys,json
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebView, QWebSettings
from PySide.QtNetwork import QNetworkRequest
from PySide.QtCore import QObject, Slot, Signal, QTimer
html_str="""<!doctype>
<html>
<body>hello world
<button id="button" >button1</button>
<button id="button2" >button2</button>
</body>
</html>
<script type="text/javascript">
document.getElementById("button").onclick=function(){
object.replyAfter10Seconds(" hello ");
}
document.getElementById("button2").onclick=function(){
object.reply2(" hello ");
}
function data_from_js(msg){
var tag=document.createElement('div');
tag.innerHTML="message from python";
document.body.appendChild(tag);
alert(msg['name']);
}
</script>
<style>
body{
border:solid black 1px;
}
</style>
</doctype>"""
class Qbutton(QObject):
def __init__(self):
super(Qbutton,self).__init__()
self.timer = QTimer()
self.timer.setSingleShot(True)
self.timer.setInterval(10 * 1000)
self.timer.timeout.connect(self.reply)
@Slot(str)
def replyAfter10Seconds(self,recd):
self._replyText = recd
print "Started timer"
self.timer.start()
@Slot()
def reply(self):
#r=QMessageBox.information(self,"Info",msg)
msgBox = QMessageBox()
msgBox.setText("python just said"+self._replyText)
msgBox.exec_()
return "I am recieving pythonic data"
#r=QMessageBox.question(self,title,recd,QMessageBox.Yes | QMessageBox.No)
@Slot(str)
def reply2(self,recd):
msgBox = QMessageBox()
msgBox.setText("python just said"+recd+ " another time")
msgBox.exec_()
return "I am recieving pythonic data"
@Slot(str)
def send_tojs(self):
pass
class adstar_gui(QWidget):
def __init__(self):
super(adstar_gui,self).__init__()
self.setWindowTitle("Adstar Wordlist Generator")
self.setMaximumWidth(5000)
self.setMaximumHeight(5000)
self.setMinimumWidth(500)
self.setMinimumHeight(500)
self.show()
print "Sample window"
def closeEvent(self,event):
self.closeEvent()
if __name__=="__main__":
Qapp=QApplication(sys.argv)
t=QWebView()
t.setHtml(html_str)
button=Qbutton()
t.page().mainFrame().addToJavaScriptWindowObject("object",button)
t.show()
t.raise_()
#t.page().mainFrame().evaluateJavaScript("data_from_js(%s);" % (json.dumps({'name':"My name is Junior"}) ))
QCoreApplication.processEvents() # does nothing as long as App.exec_() hasn't statred.
#sys.exit(Qapp.exec_())
Qapp.exec_()