概述
一、PYQT的安装
https://nchc.dl.sourceforge.net/project/pyqt/PyQt4/PyQt-4.11.4/PyQt4-4.11.4-gpl-Py3.4-Qt4.8.7-x64.exe
进入下载地址:
下载后,选中Python安装地址,pyqt将安装到对应地址的Python中
二、使用网上例子的练习
- 简单例子
import sys
from PyQt4.QtWebKit import QWebView from PyQt4.QtGui import QApplication from PyQt4.QtCore import QUrl ''' webkit简单例子 ''' app = QApplication(sys.argv)
browser = QWebView() browser.load(QUrl(sys.argv[0])) browser.show()
app.exec_() |
2.模仿浏览器(webkit浏览器例子,显示页面输入导航栏等)
import sys
from PyQt4.QtGui import QApplication from PyQt4.QtCore import QUrl from PyQt4.QtWebKit import QWebView, QWebPage from PyQt4.QtGui import QGridLayout, QLineEdit, QWidget ,QTableWidget, QHeaderView, QTableWidgetItem from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest ''' webkit浏览器例子,显示页面输入导航栏等 ''' class UrlInput(QLineEdit): def __init__(self, browser): super(UrlInput, self).__init__() self.browser = browser # add event listener on "enter" pressed self.returnPressed.connect(self._return_pressed)
def _return_pressed(self): url = QUrl(self.text()) # load url into browser frame browser.load(url)
class RequestsTable(QTableWidget): header = ["url", "status", "content-type"]
def __init__(self): super(RequestsTable, self).__init__() self.setColumnCount(3) self.setHorizontalHeaderLabels(self.header) header = self.horizontalHeader() header.setStretchLastSection(True) header.setResizeMode(QHeaderView.ResizeToContents)
def update(self, data): last_row = self.rowCount() next_row = last_row + 1 self.setRowCount(next_row) for col, dat in enumerate(data, 0): if not dat: continue self.setItem(last_row, col, QTableWidgetItem(dat))
class Manager(QNetworkAccessManager): def __init__(self, table): QNetworkAccessManager.__init__(self) # add event listener on "load finished" event self.finished.connect(self._finished) self.table = table
def _finished(self, reply): """Update table with headers, status code and url. """ headers = reply.rawHeaderPairs() headers = {str(k): str(v) for k, v in headers} content_type = headers.get("Content-Type") url = reply.url().toString() # getting status is bit of a pain status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) status = status #.toInt() self.table.update([url, str(status), content_type])
class JavaScriptEvaluator(QLineEdit): def __init__(self, page): super(JavaScriptEvaluator, self).__init__() self.page = page self.returnPressed.connect(self._return_pressed)
def _return_pressed(self): frame = self.page.currentFrame() result = frame.evaluateJavaScript(self.text())
class ActionInputBox(QLineEdit): def __init__(self, page): super(ActionInputBox, self).__init__() self.page = page self.returnPressed.connect(self._return_pressed)
def _return_pressed(self): frame = self.page.currentFrame() action_string = str(self.text()).lower() if action_string == "b": self.page.triggerAction(QWebPage.Back) elif action_string == "f": self.page.triggerAction(QWebPage.Forward) elif action_string == "s": self.page.triggerAction(QWebPage.Stop)
if __name__ == "__main__": app = QApplication(sys.argv)
grid = QGridLayout() browser = QWebView()
url_input = UrlInput(browser) requests_table = RequestsTable()
manager = Manager(requests_table) # to tell browser to use network access manager # you need to create instance of QWebPage page = QWebPage() page.setNetworkAccessManager(manager) browser.setPage(page)
grid.addWidget(url_input, 1, 0) grid.addWidget(browser, 2, 0) grid.addWidget(requests_table, 3, 0)
main_frame = QWidget() main_frame.setLayout(grid) main_frame.show()
sys.exit(app.exec_())
page = QWebPage() # ... js_eval = JavaScriptEvaluator(page)
grid.addWidget(url_input, 1, 0) grid.addWidget(browser, 2, 0) grid.addWidget(requests_table, 3, 0) grid.addWidget(js_eval, 4, 0) |
3.实现无边框最大最小化
index.html 放在和demo一个文件夹下
demo:
from PyQt4 import QtGui, QtWebKit, QtCore ''' webkit页面交互例子,无边框,页面点击最大最小化 ''' class myWindow(QtWebKit.QWebView): def __init__(self,app, parent=None): super(myWindow, self).__init__(parent) self.resize(800, 400) self.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.page().mainFrame().addToJavaScriptWindowObject('_myWindow', self) self.load(QtCore.QUrl('index.html')) self.app = app
#最大化显示 @QtCore.pyqtSlot() def showMaxsize(self): self.showMaximized()
#最小化显示 @QtCore.pyqtSlot() def showMinsize(self): self.showMinimized()
#普通大小显示 @QtCore.pyqtSlot() def showNormalsize(self): self.showNormal()
#退出程序 @QtCore.pyqtSlot() def appClose(self): app.quit()
if __name__== "__main__": import sys app = QtGui.QApplication(sys.argv) main = myWindow(app) main.show() sys.exit(app.exec_()) |
index:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>test1</title> <script> function buttonClick1(){ _myWindow.showMaximized(); } function buttonClick2(){ _myWindow.showMinimized(); } function buttonClick3(){ _myWindow.showNormalsize(); } function buttonClick4(){ _myWindow.appClose(); } </script> </head> <body> <input id="button" type="button" value="最大化" οnclick="buttonClick1();"> <input id="button" type="button" value="最小化" οnclick="buttonClick2();"> <input id="button" type="button" value="普通大小显示" οnclick="buttonClick3();"> <input id="button" type="button" value="退出程序" οnclick="buttonClick4();"> </body> </html> |
显示:
最后
以上就是简单信封为你收集整理的Python+WebKit+HTML-Domo的全部内容,希望文章能够帮你解决Python+WebKit+HTML-Domo所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复