目录
一.用Web浏览器控件(QWebEngineView)显示网页
一.用Web浏览器控件(QWebEngineView)显示网页
代码:
import sys
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class WebEngineView(QMainWindow):
def __init__(self,parent=None):
super(WebEngineView, self).__init__(parent)
self.setWindowTitle('打开网页的例子')
self.setGeometry(5,30,1355,730)
self.browser=QWebEngineView()
self.browser.load(QUrl('http://www.jd.com'))
self.setCentralWidget(self.browser)
if __name__=='__main__':
app=QApplication(sys.argv)
main=WebEngineView()
main.show()
sys.exit(app.exec_())
运行结果:
二.装载本地Web页面
代码:
html文件(test.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试页面</title>
</head>
<body>
<h1>Hello PyQt5</h1>
<h2>Hello PyQt5</h2>
<h3>Hello PyQt5</h3>
<h4>Hello PyQt5</h4>
</body>
</html>
py文件:
import sys
import os
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class localHTML(QMainWindow):
def __init__(self,parent=None):
super(localHTML, self).__init__(parent)
self.setWindowTitle('装载本地Web页面')
self.setGeometry(5,30,1355,730)
#os.getcwd()是获取当前路径
print(os.getcwd())
url=os.getcwd()+'/test.html'
self.browser=QWebEngineView()
#QUrl.fromLocalFile(url)
self.browser.load(QUrl.fromLocalFile(url))
self.setCentralWidget(self.browser)
if __name__=='__main__':
app=QApplication(sys.argv)
main=localHTML()
main.show()
sys.exit(app.exec_())
运行结果:
三.显示嵌入Web页面
将web页面的代码硬编码到代码里面,然后显示。
代码:
import sys
import os
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class innerHTML(QMainWindow):
def __init__(self,parent=None):
super(innerHTML, self).__init__(parent)
self.setWindowTitle('装载本地Web页面')
self.setGeometry(5,30,1355,730)
self.browser=QWebEngineView()
#直接嵌入页面源码
self.browser.setHtml('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试页面</title>
</head>
<body>
<h1>Hello PyQt5</h1>
<h2>Hello PyQt5</h2>
<h3>Hello PyQt5</h3>
<h4>Hello PyQt5</h4>
</body>
</html>
''')
self.setCentralWidget(self.browser)
if __name__=='__main__':
app=QApplication(sys.argv)
main=innerHTML()
main.show()
sys.exit(app.exec_())
运行结果:
效果和上面一样。
下面俩不懂JavaScript看不懂,也不想看。。。哈哈过完年再说
四.PyQt5调用JavaScript代码,并返回值
PyQt5和JavaScript交互
什么叫交互
PyQt5 <-> JavaScript
eg: PyQt5调用JavaScript的函数,然后JavaScript的函数返回了值给PyQt5。
代码:
全搞懂了再贴。。
运行结果:
功能:分别输入First name和Last name,然后 PyQt5调用JavaScript的函数,返回全名。
五.JavaScript调用PyhtonAPI计算阶乘
将Python的一个对象映射到JavaScript中
将槽函数映射到JavaScript中
代码:
有bug,在网页上输入之后没有响应。
四个文件:
1.qwebchannel.js文件太长已上传,戳这里(粉丝即可下载)
2.js调用py文件(js_call_py.html)
<html>
<head>
<title>A Demo Page</title>
<meta charset="UTF-8">
<script src="./qwebchannel.js"></script>
<script language="javascript">
function callback(result) {
alert('计算结果:' + result)
}
document.addEventListener("DOMContentLoaded", function () {
new QWebChannel( qt.webChannelTransport, function(channel) {
window.obj = channel.objects.obj;
});
});
function onFactorial() {
if ( window.obj) {
var n = parseInt(document.getElementById('n').value);
window.obj.factorial(n,callback)
}
}
</script>
</head>
<body>
<form>
<label >请输入N:</label>
<input type="text" id="n"></input>
<br />
<input type="button" value="计算阶乘" onclick="onFactorial()">
</form>
</body>
</html>
3.阶乘功能类(factorial.py)
from PyQt5.QtCore import *
class Factorial(QObject):
@pyqtSlot(int, result=int)
def factorial(self,n):
if n == 0 or n == 1:
return 1
else:
return self.factorial(n - 1) * n
4.调用类(PyFactorial.py)
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys
import os
from factorial import *
channel = QWebChannel()
factorial = Factorial()
class PyFactorial(QWidget):
def __init__(self):
super(PyFactorial, self).__init__()
self.setWindowTitle('Python计算阶乘')
self.resize(600,300)
layout=QVBoxLayout()
self.browser = QWebEngineView()
url = os.getcwd() + '/js_call_py.html'
#装载本地html文件
self.browser.load(QUrl.fromLocalFile(url))
channel.registerObject("obj",factorial)
self.browser.page().setWebChannel(channel)
layout.addWidget(self.browser)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = PyFactorial()
win.show()
sys.exit(app.exec_())
运行结果: