Spaces:
Runtime error
Runtime error
File size: 2,234 Bytes
3c940b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon
class Browser(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Simple Browser")
self.setGeometry(100, 100, 1200, 800)
# Create web view
self.web_view = QWebEngineView()
self.web_view.setUrl(QUrl("https://www.google.com"))
# Create central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.addWidget(self.web_view)
# Create toolbar
toolbar = QToolBar()
self.addToolBar(toolbar)
# Back button
back_btn = QAction("Back", self)
back_btn.setStatusTip("Go back")
back_btn.triggered.connect(self.web_view.back)
toolbar.addAction(back_btn)
# Forward button
forward_btn = QAction("Forward", self)
forward_btn.setStatusTip("Go forward")
forward_btn.triggered.connect(self.web_view.forward)
toolbar.addAction(forward_btn)
# Refresh button
refresh_btn = QAction("Refresh", self)
refresh_btn.setStatusTip("Refresh page")
refresh_btn.triggered.connect(self.web_view.reload)
toolbar.addAction(refresh_btn)
# URL bar
self.url_bar = QLineEdit()
self.url_bar.setPlaceholderText("Enter URL")
self.url_bar.returnPressed.connect(self.navigate_to_url)
toolbar.addWidget(self.url_bar)
# Update URL bar when page changes
self.web_view.urlChanged.connect(self.update_url_bar)
# Show status bar
self.statusBar()
def navigate_to_url(self):
url = self.url_bar.text()
if not url.startswith("http"):
url = "http://" + url
self.web_view.setUrl(QUrl(url))
def update_url_bar(self, qurl):
self.url_bar.setText(qurl.toString())
if __name__ == "__main__":
app = QApplication(sys.argv)
browser = Browser()
browser.show()
sys.exit(app.exec_()) |