Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Navegador simples em Python.
- Permite carregar sites através de Abas, Gerenciar sites Favoritos: Cadastrar, Editar e Excluir. Obs: Os favoritos ficam salvos no arquivo favoritos.txt
- Código para fins de estudo.
- 08/06/2023 - Mizuno
- --------------------------------------------------------------------------------------------------
- import sys
- from PyQt5.QtCore import *
- from PyQt5.QtWebEngineWidgets import *
- from PyQt5.QtWidgets import *
- class TabWidget(QTabWidget):
- def __init__(self):
- super(TabWidget, self).__init__()
- self.setTabsClosable(True)
- self.tabCloseRequested.connect(self.close_tab)
- self.favorites = []
- self.new_tab()
- def new_tab(self, url='http://google.com'):
- browser = QWebEngineView()
- browser.setUrl(QUrl(url))
- self.addTab(browser, 'New Tab')
- self.setCurrentWidget(browser)
- browser.urlChanged.connect(self.update_url)
- def close_tab(self, index):
- widget = self.widget(index)
- widget.deleteLater()
- self.removeTab(index)
- def update_url(self, q):
- browser = self.sender()
- index = self.indexOf(browser)
- self.setTabText(index, q.toString())
- def save_favorite(self):
- browser = self.currentWidget()
- page_title = browser.title()
- page_url = browser.url().toString()
- self.favorites.append((page_title, page_url))
- self.update_favorites_file()
- QMessageBox.information(self, 'Sucesso', 'Favorito salvo com sucesso!')
- def load_favorites(self):
- self.favorites.clear()
- try:
- with open('favoritos.txt', 'r') as file:
- lines = file.readlines()
- for line in lines:
- parts = line.strip().split(',')
- if len(parts) == 2:
- page_title, page_url = parts
- self.favorites.append((page_title, page_url))
- except FileNotFoundError:
- QMessageBox.warning(self, 'Erro', 'Arquivo de favoritos não encontrado.')
- self.show_favorites_dialog()
- def show_favorites_dialog(self):
- dialog = QDialog()
- dialog.setWindowTitle('Favoritos')
- dialog.setModal(True)
- layout = QVBoxLayout()
- dialog.setLayout(layout)
- list_widget = QListWidget()
- list_widget.setSpacing(5)
- for page_title, page_url in self.favorites:
- list_widget.addItem(QListWidgetItem(page_title))
- layout.addWidget(list_widget)
- button_box = QDialogButtonBox()
- open_button = button_box.addButton('Abrir', QDialogButtonBox.ActionRole)
- edit_button = button_box.addButton('Editar', QDialogButtonBox.ActionRole)
- delete_button = button_box.addButton('Excluir', QDialogButtonBox.ActionRole)
- close_button = button_box.addButton('Fechar', QDialogButtonBox.RejectRole)
- layout.addWidget(button_box)
- def open_favorite():
- selected_item = list_widget.currentItem()
- if selected_item:
- index = list_widget.row(selected_item)
- _, page_url = self.favorites[index]
- browser = self.currentWidget()
- browser.setUrl(QUrl(page_url))
- dialog.close()
- def edit_favorite():
- selected_item = list_widget.currentItem()
- if selected_item:
- index = list_widget.row(selected_item)
- page_title, page_url = self.favorites[index]
- edit_dialog = QDialog()
- edit_dialog.setWindowTitle('Editar Favorito')
- edit_dialog.setModal(True)
- edit_layout = QVBoxLayout()
- edit_dialog.setLayout(edit_layout)
- edit_name = QLineEdit(page_title)
- edit_url = QLineEdit(page_url)
- edit_layout.addWidget(QLabel('Nome:'))
- edit_layout.addWidget(edit_name)
- edit_layout.addWidget(QLabel('URL:'))
- edit_layout.addWidget(edit_url)
- save_button = QPushButton('Salvar')
- edit_layout.addWidget(save_button)
- def save_edited_favorite():
- new_title = edit_name.text()
- new_url = edit_url.text()
- if new_title and new_url:
- self.favorites[index] = (new_title, new_url)
- self.update_favorites_file()
- selected_item.setText(new_title)
- QMessageBox.information(self, 'Sucesso', 'Favorito atualizado com sucesso!')
- edit_dialog.close()
- else:
- QMessageBox.warning(self, 'Erro', 'Preencha todos os campos.')
- save_button.clicked.connect(save_edited_favorite)
- edit_dialog.exec_()
- def delete_favorite():
- selected_item = list_widget.currentItem()
- if selected_item:
- index = list_widget.row(selected_item)
- confirm_dialog = QMessageBox.question(self, 'Excluir Favorito',
- 'Tem certeza que deseja excluir este favorito?',
- QMessageBox.Yes | QMessageBox.No)
- if confirm_dialog == QMessageBox.Yes:
- self.favorites.pop(index)
- self.update_favorites_file()
- list_widget.takeItem(list_widget.row(selected_item))
- QMessageBox.information(self, 'Sucesso', 'Favorito excluído com sucesso!')
- def close_dialog():
- dialog.close()
- open_button.clicked.connect(open_favorite)
- edit_button.clicked.connect(edit_favorite)
- delete_button.clicked.connect(delete_favorite)
- close_button.clicked.connect(close_dialog)
- dialog.exec_()
- def update_favorites_file(self):
- with open('favoritos.txt', 'w') as file:
- for page_title, page_url in self.favorites:
- file.write(f"{page_title},{page_url}\n")
- class MainWindow(QMainWindow):
- def __init__(self):
- super(MainWindow, self).__init__()
- self.tab_widget = TabWidget()
- self.setCentralWidget(self.tab_widget)
- self.showMaximized()
- navbar = QToolBar()
- self.addToolBar(navbar)
- new_tab_btn = QAction('Nova Aba', self)
- new_tab_btn.triggered.connect(lambda: self.tab_widget.new_tab())
- navbar.addAction(new_tab_btn)
- save_favorite_btn = QAction('Salvar Favorito', self)
- save_favorite_btn.triggered.connect(self.tab_widget.save_favorite)
- navbar.addAction(save_favorite_btn)
- load_favorites_btn = QAction('Carregar Favoritos', self)
- load_favorites_btn.triggered.connect(self.tab_widget.load_favorites)
- navbar.addAction(load_favorites_btn)
- back_btn = QAction('Voltar', self)
- back_btn.triggered.connect(self.current_browser().back)
- navbar.addAction(back_btn)
- forward_btn = QAction('Avançar', self)
- forward_btn.triggered.connect(self.current_browser().forward)
- navbar.addAction(forward_btn)
- reload_btn = QAction('Recarregar', self)
- reload_btn.triggered.connect(self.current_browser().reload)
- navbar.addAction(reload_btn)
- home_btn = QAction('Home', self)
- home_btn.triggered.connect(self.navigate_home)
- navbar.addAction(home_btn)
- self.url_bar = QLineEdit()
- self.url_bar.returnPressed.connect(self.navigate_to_url)
- navbar.addWidget(self.url_bar)
- copy_btn = QAction('Copiar URL', self)
- copy_btn.triggered.connect(self.copy_url)
- navbar.addAction(copy_btn)
- self.tab_widget.currentChanged.connect(self.update_url)
- def current_browser(self):
- return self.tab_widget.currentWidget()
- def navigate_home(self):
- self.current_browser().setUrl(QUrl('http://google.com'))
- def navigate_to_url(self):
- url = self.url_bar.text()
- self.current_browser().setUrl(QUrl(url))
- def update_url(self, index):
- browser = self.tab_widget.widget(index)
- self.url_bar.setText(browser.url().toString())
- def copy_url(self):
- clipboard = QApplication.clipboard()
- clipboard.setText(self.url_bar.text())
- QMessageBox.information(self, 'Sucesso', 'URL copiada para a área de transferência!')
- app = QApplication(sys.argv)
- QApplication.setApplicationName('Navegador com Abas')
- window = MainWindow()
- app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement