我有一个示例pyside演示程序,用来查看webkit浏览器与python通信... 在webkit中我有两个按钮。
按钮1 - 点击后休眠10秒钟,然后打印一条消息
按钮2 - 点击后立即打印一条消息。
当我点击按钮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 在后台执行一些操作,而不必等待 button 1
函数完成(以便 button 2
函数无需等待)?
请使用此演示并对其进行改进...非常感谢