Python ile Web Browser yapmak

Egehan KAHRAMAN

 Python  ile  Web Browser yapmak


BÖLÜM(2:BİR KAÇ EKLENTİ)


 bu yazıda daha önce yaptığımız tarayıcıyı biraz daha geliştirmek nasıl  olacak bakalım

yukarıda tarayıcının ekran görüntüsü bulunmakta 

özellikler:

  1. Dosya menüsü
  2. Ayarlar sayfası
  3. Dosya açabilme
  4. Sekme ismini sitenin ismi yapmak
Yukarıda özellikler eklendi.


Kaynak kodları:



from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QFileDialog, QLineEdit,QHBoxLayout,QAction, QMenu, QMenuBar, QPushButton, QTabWidget, QVBoxLayout, QWidget
import sys
import os
from PyQt5.QtWidgets import QCheckBox, QLabel, QLineEdit, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
from VBM_SYS import Browser_sys

class Browser(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
# Set the window title
self.setWindowTitle("VastSea Browser 1.2")

# Set the background color to black
self.setStyleSheet("background-color: gray;")


self.tabs = QTabWidget()

self.tabs.setTabsClosable(True)

# Connect the tabCloseRequested signal to the closeTab slot
self.tabs.tabCloseRequested.connect(self.closeTab)
# Create the tab widget

# Create the new tab button
self.new_tab_button = QPushButton("+")

# Connect the new tab button to the addTab slot
self.new_tab_button.clicked.connect(self.addTab)

# Create the back button
self.back_button = QPushButton(""<"")

# Connect the back button to the goBack slot
self.back_button.clicked.connect(self.goBack)

# Create the forward button
self.forward_button = QPushButton(">")

# Connect the forward button to the goForward slot
self.forward_button.clicked.connect(self.goForward)

# Create the refresh button
self.refresh_button = QPushButton("R")

# Connect the refresh button to the refreshPage slot
self.refresh_button.clicked.connect(self.refreshPage)

self.url_bar = QLineEdit()

# Connect the URL bar to the loadUrl slot
self.url_bar.returnPressed.connect(self.loadUrl)
self.url_bar.returnPressed.connect(self.goBack)
# Create the menu bar
self.menu_bar = QMenuBar()

# Create the file menu
self.file_menu = QMenu("Dosya")

# Create the new tab action
self.new_tab_action = QAction("Yeni Sekme", self)
self.new_tab_action.triggered.connect(self.addTab)

# Create the new window action
self.new_window_action = QAction("Yeni pencere", self)
self.new_window_action.triggered.connect(self.addWindow)

# Create the open action
self.open_action = QAction("Aç", self)
self.open_action.triggered.connect(self.open)

# Create the close action
self.close_action = QAction("Kapat", self)
self.close_action.triggered.connect(self.close)

self.close_action = QAction("Ayarlar", self)
self.close_action.triggered.connect(self.sesting)

# Add the actions to the file menu
self.file_menu.addAction(self.new_tab_action)
self.file_menu.addAction(self.new_window_action)
self.file_menu.addAction(self.open_action)
self.file_menu.addAction(self.close_action)

# Add the file menu to the menu bar
self.menu_bar.addMenu(self.file_menu)

layout = QVBoxLayout()

# Create the navigation bar layout
nav_bar = QHBoxLayout()
nav_bar.addWidget(self.menu_bar)
nav_bar.addWidget(self.back_button)
nav_bar.addWidget(self.forward_button)
nav_bar.addWidget(self.refresh_button)
nav_bar.addWidget(self.new_tab_button)
nav_bar.addWidget(self.url_bar)



# Add the navigation bar and the tab widget to the layout
layout.addLayout(nav_bar)
layout.addWidget(self.tabs)


# Set the layout
self.setLayout(layout)


def addTab(self):
# Create a new tab
tab = QWidget()
view = QWebEngineView()
view.setStyleSheet("background-color: gray;")
view.load(QUrl("https://www.google.com"))
layout = QVBoxLayout()
layout.addWidget(view)

# Connect the titleChanged signal to the updateTabName slot
view.titleChanged.connect(self.updateTabName)

tab.setLayout(layout)

# Add the tab to the tab widget
self.tabs.addTab(tab, "Yeni Pencere")

def updateTabName(self, title):
# Get the index of the current tab
index = self.tabs.currentIndex()

# Set the tab name to the webpage title
self.tabs.setTabText(index, title)

def addWindow(self):
# Create a new window
window = Browser()
window.show()

def sesting(self):

tab = Browser_sys()

# Add the tab to the tab widget
self.tabs.addTab(tab, "Settings")


def open(self):
# Open a file
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
file_name, _ = QFileDialog.getOpenFileName(self, "Dosya aç", "", "", options=options)
if file_name:
# Load the file into the current tab
view = self.tabs.currentWidget().findChild(QWebEngineView)
view.load(QUrl.fromLocalFile(file_name))

def close(self):
# Close the current tab
self.tabs.removeTab(self.tabs.currentIndex())

def loadUrl(self):
# Get the URL from the URL bar
url = self.url_bar.text()

# Load the URL into the current tab
view = self.tabs.currentWidget().findChild(QWebEngineView)
view.load(QUrl(url))
def goBack(self):
# Get the current tab
tab = self.tabs.currentWidget()

# Get the web view in the current tab
view = tab.findChild(QWebEngineView)

# Go back to the previous page
view.back()
def goForward(self):
# Get the current tab
tab = self.tabs.currentWidget()

# Get the web view in the current tab
view = tab.findChild(QWebEngineView)

# Go forward to the next page
view.forward()
def refreshPage(self):
# Get the current tab
tab = self.tabs.currentWidget()

# Get the web view in the current tab
view = tab.findChild(QWebEngineView)

# Reload the current page
view.reload()
def closeTab(self, index):
# Close the tab at the given index
self.tabs.removeTab(index)

app = QApplication(sys.argv)
browser = Browser()
browser.show()
sys.exit(app.exec_())




Yorum Gönder

0Yorumlar
Yorum Gönder (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !