Displaying Websites with Python - python

I am making an os in python, but I need a web browser. Currently I am using the os.startfile method to launch chrome, but I want another way. I want a program that a user can enter a webpage and displaying the web page without using chrome, firefox, safari etc.
Here is the basic framework I have:
from tkinter import *
import webbrowser as wb
window = Tk()
window.configure(bg="Dark Red")
window.geometry("1000x1000")
window.title("Hyper Web Browser")
window.iconbitmap("icon.ico")
''' Defined Functions'''
def submit_url():
wb.open_new_tab(Address_Bar.get())
file2write = open("History.txt", "a")
file2write.write(["\n", Address_Bar.get()])
return submit_url
'''Objects'''
Address_Bar = Entry(
bg="White",
bd=0,
font=("Comic", 25),
width=100
)
Tab_1 = Label(
bg="Red",
bd=0,
width=20,
height=3
)
Return = Button(
command=submit_url()
)
Address_Bar.place(x=20, y=60)
Tab_1.place(x=0, y=0)
Return.pack()
window.mainloop()
However, this program launches the web page into the user's default browser. Hence, I want to display the web page without using any other web browsers.

Here is a simpler version of webbrowser using PyQt5 :
import sys
from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtWebEngineWidgets import *
app=QtWidgets.QApplication(sys.argv)
w=QWebEngineView()
w.load(QtCore.QUrl('https://google.com')) ## load google on startup
w.showMaximized()
app.exec_()
You can now add different widgets to it .
In python you have two most common ways to make a webbrowser 1. by using gtk webkit 2. by QtWebEngine under PyQt5.
Webkit is based upon Safari while QtWebEngine is based on Chromium. You can decide which one suits you the best. Hope it helps.

Something like this may be what you are looking for. This is a simple demo script that allows web browsing, without opening it in a default web browser such as chrome. (This is essentially a DIY web browser script) I hope this helps!
from functools import cached_property
import sys
import keyboard
from prompt_toolkit.key_binding import KeyBindings
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
bindings = KeyBindings()
class WebView(QtWebEngineWidgets.QWebEngineView):
def createWindow(self, type_):
if not isinstance(self.window(), Browser):
return
if type_ == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
return self.window().tab_widget.create_tab()
class TabWidget(QtWidgets.QTabWidget):
def create_tab(self):
view = WebView()
index = self.addTab(view, "...")
self.setTabIcon(index, view.icon())
view.titleChanged.connect(
lambda title, view=view: self.update_title(view, title)
)
view.iconChanged.connect(lambda icon, view=view: self.update_icon(view, icon))
self.setCurrentWidget(view)
return view
def update_title(self, view, title):
index = self.indexOf(view)
if 'DuckDuckGo' in title:
self.setTabText(index, 'Search')
else:
self.setTabText(index, title)
def update_icon(self, view, icon):
index = self.indexOf(view)
self.setTabIcon(index, icon)
class Browser(QtWidgets.QMainWindow):
def __init__(self, parent=None):
#main browser functrion
super().__init__(parent)
self.setCentralWidget(self.tab_widget)
view = self.tab_widget.create_tab()
view.load(QtCore.QUrl("https://www.duckduckgo.com/"))
QtWebEngineWidgets.QWebEngineProfile.defaultProfile().downloadRequested.connect(self.on_downloadRequested)
#QtCore.pyqtSlot("QWebEngineDownloadItem*")
def on_downloadRequested(self, download):
old_path = download.url().path()
suffix = QtCore.QFileInfo(old_path).suffix()
path, _ = QtWidgets.QFileDialog.getSaveFileName(
self, "Save File", old_path, "*." + suffix
)
if path:
download.setPath(path)
download.accept()
#cached_property
def tab_widget(self):
return TabWidget()
def main():
app = QtWidgets.QApplication(sys.argv)
w = Browser()
w.show()
w.showMaximized()
sys.exit(app.exec_())
if __name__ == "__main__":
while True:
main()

Related

PYQT6 and QT designer, after opening second window, buttons wont work

So i am making a GUI to identify seagulls.
I have made some QMainWindow's and would like the user to nagviate through these using buttons.
The first window works fine, and the user gets to the next page.
However, when clicking on buttons on page 2, nothing works, not even printing a simple statement. I am new to this and i am doing something wrong. I tried messing with init but am at a loss.
I am using a function
def openNewPage(self,b):
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
to open a new page by having the buttons clicked on by the user have the name of the next file to be opened.
For example: the button for white head has the text "WhiteHead"
and will the open the file WhiteHead.ui
example:
self.bWhiteHead.clicked.connect(lambda:self.openNewPage(self.bWhiteHead))
Please help me understand why buttons work in one window and not in the next, thank you.
Here is the code:
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import Qt
from PyQt6 import uic
import time
class MyApp(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("SeagullPage1.ui",self)
self.setWindowTitle("SEAGULL IDENTIFIER")
self.bBlackHead.clicked.connect(lambda:self.openNewPage(self.bBlackHead))
self.bWhiteHead.clicked.connect(lambda:self.openNewPage(self.bWhiteHead))
def openNewPage(self,b):
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
class WhiteHead(QMainWindow):
def __init__(self):
super().__init__
self.setWindowTitle("SEAGULL IDENTIFIER2")
self.BlackFeetButton.clicked.connect(lambda:self.openNewPage(self.BlackFeetButton))
self.PinkFeetButton.clicked.connect(lambda:self.openNewPage(self.PinkFeetButton))
self.YellowFeetButton.clicked.connect(lambda:self.openNewPage(self.YellowFeetButton))
def openNewPage(self,b):
print("hey")
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
class YellowFeet(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("SEAGULL IDENTIFIER3")
self.RedBeakButton.clicked.connect(lambda:self.openNewPage(self.RedBeakButton))
self.NonReadBeakButton.clicked.connect(lambda:self.openNewPage(self.NonReadBeakButton))
def openNewPage(self,b):
print("hey")
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
if __name__ == "__main__":
app = QApplication(sys.argv)
myApp = MyApp()
myApp.show()
app.exec()
#sys.exit(app.exec_())

Automate webbrowser

I'm trying to create a dedicated web browser for my work with some buttons which can automate tasks for me. The script I found is like below, but I'm struggling with the piece that would enter text in de search field and then hit the button. (This is a test piece ofcourse)
Does anyone know how to automate this, using the pyqt5 example found?
Something like:
self.browser.getObjectbyName("ObjName").setText("newtext")
I've been googling for hours but have not found any example code yet.
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow,self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("http://www.google.com"))
self.setCentralWidget(self.browser)
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
There are several tools in QtWebEngine to perform automated tasks on web pages:
Execute js script using runJavaScript() method.
Use QtWebChannel to exchange information and perform tasks between python and the web page.
Implement userscripts through QWebEngineScript.
In this case I will show a demo of how to do an automated search using the first option:
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys
script = """
var input = document.querySelector("body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input")
input.value = "StackOverflow"
var form = document.querySelector("body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form")
form.submit()
"""
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.loadFinished.connect(self.handle_load_finished)
self.browser.setUrl(QUrl("http://www.google.com"))
self.setCentralWidget(self.browser)
def handle_load_finished(self, ok):
if ok:
if self.browser.url() == QUrl("https://www.google.com/"):
self.browser.page().runJavaScript(script)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

Why dont my buttons work in my pyqt5 GUI?

When I made a GUI i used different classes to construct the main UI Screen.
My code has the following structure:
This is the GUI itself:
The bottum_buttons.py creates the 3 buttons at the buttom. This is the code that is inside bottum_buttons.py:
import Advanced_window
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
import calendar
import sys
class bottum_buttons(QWidget):
def __init__(self):
QWidget.__init__(self)
# Create Layout
self.bottum_box = QHBoxLayout()
# Creating Buttons
self.cancel_button = QPushButton("Cancel")
self.run_button = QPushButton("Run")
self.advanced_button = QPushButton("Advancend Options")
self.add_items_to_layout()
self.create_button_functions()
def add_items_to_layout(self):
self.bottum_box.addWidget(self.cancel_button)
self.bottum_box.addWidget(self.run_button)
self.bottum_box.addWidget(self.advanced_button)
def create_button_functions(self):
self.cancel_button.clicked.connect(self.close)
self.advanced_button.clicked.connect(Advanced_window.advancedwindows)
def return_bottum_buttons(self):
return self.bottum_box
My code that actually constructs the GUI is inside main_screen.py.
The following code is inside this file:
from Ui_Elements import option_box
from Ui_Elements import path_box
from Ui_Elements import bottum_buttons
from Ui_Elements import command_output
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
import calendar
import sys
class main_screen(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setWindowTitle("Robo Tool")
self.main_frame = QVBoxLayout()
# Get UI Elements
path_ui = path_box.path_box()
option_ui = option_box.option_box()
command_ui = command_output.command_box()
bottum_ui = bottum_buttons.bottum_buttons()
self.path = path_ui.return_path_box()
self.option_box = option_ui.return_options_box()
self.command_output = command_ui.return_command_box()
self.bottum_buttons = bottum_ui.return_bottum_buttons()
self.setLayout(self.add_item_to_frame(self.main_frame))
def add_item_to_frame(self, main_frame):
main_frame.addLayout(self.path)
main_frame.addLayout(self.option_box)
main_frame.addLayout(self.command_output)
main_frame.addLayout(self.bottum_buttons)
return main_frame
app = QApplication(sys.argv)
dialog = main_screen()
dialog.show()
app.exec_()
Now the problem is. When i start the main_screen.py the GUI shows up as the picture provided. But the buttons don't work. I dont get any error message. They're still clickable but they dont run the command i provided. Can somebody please help me out.
I don't know what Advanced_window.advancedwindows() is supposed to do, but your cancel button is connected to bottom_buttons.close, not to main_screen.close which I assume is what you want. Since bottom_buttons has no knowledge in advance about which window is supposed to be closed, you can't really connect the button to the close method of a predefined widget. What you could do however is to use self.window().close() instead which would close the next-level ancestor widget of bottom_buttons that has a window. For this, you would need to set the layout of bottom_bottuns to self.bottom_box and add the whole widget to the layout of main_screen rather than just the layout.
This would mean that you would get something like this for bottom_buttons:
class bottum_buttons(QWidget):
def __init__(self):
.... as before ....
# set bottom_box as layout of self
self.setLayout(self.bottom_box)
....
def create_button_functions(self):
# connect cancel button to close method of top level ancestor widget
self.cancel_button.clicked.connect(lambda: self.window().close())
....
And for main_screen:
class main_screen(QDialog):
def __init__(self):
.... as before ....
# set self.bottom_buttons to bottom_buttons widget rather than the layout
self.bottum_buttons = bottum_ui
self.setLayout(self.add_item_to_frame(self.main_frame))
def add_item_to_frame(self, main_frame):
...
# add bottom_buttons widget to the layout.
main_frame.addWidget(self.bottum_bottons)
return main_frame

How to open hyperlink with target="_blank" in PyQtWebEngine?

I have made a web browser using pyqt5 and PyQtWebEngine.It works fine but when I click on a hyperlink with target="_blank" then it does not work but how will I fix it. You can view its source code by clicking on this link https://github.com/SaptakBhoumik/WebPlus
. Please review my code and tell me what to do
As noted in the docs:
_blank: usually a new tab, but users can configure browsers to open a new window instead.
That is, the objective is not to reload the page but to create a new tab, and then to obtain that request you must override the createWindow method of QWebEngineView (or QWebEnginePage):
from functools import cached_property
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class WebView(QtWebEngineWidgets.QWebEngineView):
def createWindow(self, type_):
if not isinstance(self.window(), Browser):
return
if type_ == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
return self.window().tab_widget.create_tab()
class TabWidget(QtWidgets.QTabWidget):
def create_tab(self):
view = WebView()
index = self.addTab(view, "(Untitled)")
self.setTabIcon(index, view.icon())
view.titleChanged.connect(
lambda title, view=view: self.update_title(view, title)
)
view.iconChanged.connect(lambda icon, view=view: self.update_icon(view, icon))
self.setCurrentWidget(view)
return view
def update_title(self, view, title):
index = self.indexOf(view)
self.setTabText(index, title)
def update_icon(self, view, icon):
index = self.indexOf(view)
self.setTabIcon(index, icon)
class Browser(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setCentralWidget(self.tab_widget)
view = self.tab_widget.create_tab()
view.load(
QtCore.QUrl(
"https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target"
)
)
#cached_property
def tab_widget(self):
return TabWidget()
def main():
app = QtWidgets.QApplication(sys.argv)
w = Browser()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Note: I recommend that you review the official example: WebEngine Widgets Simple Browser Example, in addition to its implementation in PySide2 which is easily translatable to PyQt5 where this feature and many more are implemented.

How to get submenu In Pyqt5 .ui file?

I want to get the submenu That is QuitProgram <- This is name of submenu but it is in UI file.
How can I get it in a variable set its action to quit program?
File menu object name is 'actionQuit_FromProgram' <- This is a submenu.
Python File:
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QFileDialog, QMenuBar
from PyQt5.QtGui import *
import sys
import os
import qdarkgraystyle
path = os.path.abspath(os.getcwd())
import qrcode
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('main.ui', self)
#self.show()
self.button = self.findChild(QtWidgets.QPushButton, 'qrgenerator')
self.button.clicked.connect(self.QrCodeGenerator) # Remember to pass the definition/method, not the return value!
self.input = self.findChild(QtWidgets.QLineEdit, 'qredit')
self.button1 = self.findChild(QtWidgets.QPushButton, 'qropen')
self.button1.clicked.connect(self.OpenQRCode)
self.menu1 = self.findChild(QMenuBar,'actionQuit_FromProgram')
print(self.menu1)
#self.menu1.triggered.connect(qApp.quit)
self.show()
def QrCodeGenerator(self):
# Generate QR code
self.url = qrcode.make(self.qredit.text())
if self.qredit.text() == '':
QMessageBox.warning(self, "Error", "Please Type In Something To Generate Qr Code")
else:
self.url.save("filename.png","PNG")
def OpenQRCode(self):
fname = QFileDialog.getOpenFileName(self, 'Open file',
path,"Image files (*.jpg *.gif *.png *.svg)")[0]
self.label_2.setPixmap(QPixmap(fname))
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(qdarkgraystyle.load_stylesheet())
window = Ui()
app.exec_()
If you're using loadUi (or setupUi if you're using files generated by pyuic), all elements in the object inspector (the tree view that lists all widgets on your UI) become available as instance attributes, according to their object name.
So, if your action is called actionQuit_FromProgram in the inspector, you can directly access it using self.actionQuit_FromProgram.
This also means that:
all those findChild are absolutely useless: you already have access to self.qrgenerator, self.qredit, etc;
in any case, findChild should be used with the correct class of the object you're looking for: I sincerely doubt that actionQuit_FromProgram is a QMenuBar (so, using findChild(QMenuBar, ...) won't work at all; if it is an action, use findChild(QAction, ...); if it's a submenu, use findChild(Qmenu, ...);
there's usually just one menu bar for each QMainWindow, and it is easily accessible using self.menuBar();

Categories