用PyQt4打造一个简易浏览器
2009 7 2 11:16 AM 13850次查看
支持JavaScript和CSS,Acid3浏览器兼容性测试能跑41分(IE 6才5分),地址如果没输全的话,会自动在前面加上http://。
参考了这篇文章:http://ogc-daily.blogspot.com/2009/04/pyqt-part-7.html
界面:
源代码如下(需要安装PyQt4):
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
class MyBrowser(QWidget):
def __init__(self, parent = None):
super(MyBrowser, self).__init__(parent)
self.createLayout()
self.createConnection()
def search(self):
address = str(self.addressBar.text())
if address:
if address.find('://') == -1:
address = 'http://' + address
url = QUrl(address)
self.webView.load(url)
def createLayout(self):
self.setWindowTitle("keakon's browser")
self.addressBar = QLineEdit()
self.goButton = QPushButton("&GO")
bl = QHBoxLayout()
bl.addWidget(self.addressBar)
bl.addWidget(self.goButton)
self.webView = QWebView()
layout = QVBoxLayout()
layout.addLayout(bl)
layout.addWidget(self.webView)
self.setLayout(layout)
def createConnection(self):
self.connect(self.addressBar, SIGNAL('returnPressed()'), self.search)
self.connect(self.addressBar, SIGNAL('returnPressed()'), self.addressBar, SLOT('selectAll()'))
self.connect(self.goButton, SIGNAL('clicked()'), self.search)
self.connect(self.goButton, SIGNAL('clicked()'), self.addressBar, SLOT('selectAll()'))
app = QApplication(sys.argv)
browser = MyBrowser()
browser.show()
sys.exit(app.exec_())
向下滚动可载入更多评论,或者点这里禁止自动加载。