B Mine
Знаток
(257)
1 месяц назад
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton, QVBoxLayout, QWidget, QToolBar
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Browser(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Простой веб-браузер")
self.setGeometry(200, 200, 1200, 800)
self.browser = QWebEngineView()
self.browser.setUrl('https://www.google.com') # Начальная страница
self.url_bar = QLineEdit()
self.url_bar.returnPressed.connect(self.navigate_to_url)
back_button = QPushButton("<- Назад")
back_button.clicked.connect(self.browser.back)
forward_button = QPushButton("Вперед ->")
forward_button.clicked.connect(self.browser.forward)
toolbar = QToolBar()
toolbar.addWidget(back_button)
toolbar.addWidget(forward_button)
toolbar.addWidget(self.url_bar)
self.browser.urlChanged.connect(self.update_url_bar)
self.layout = QVBoxLayout()
self.layout.addWidget(toolbar)
self.layout.addWidget(self.browser)
container = QWidget()
container.setLayout(self.layout)
self.setCentralWidget(container)
self.show()
def navigate_to_url(self):
url = self.url_bar.text()
if not url.startswith('http'):
url = f'https://{url}' # Добавляем http, если нет
self.browser.setUrl(url)
def update_url_bar(self, q):
self.url_bar.setText(q.toString())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Browser()
sys.exit(app.exec_())