4

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.

demongolem
  • 9,474
  • 36
  • 90
  • 105
NeelJ 83
  • 149
  • 1
  • 2
  • 6
  • 3
    Any program that displays a Web page is called a Web browser. You attempt to implement your own Web browser. Good luck. – DYZ Mar 15 '17 at 20:48
  • this might be useful: http://stackoverflow.com/questions/15138614/how-can-i-read-the-contents-of-an-url-with-python – somesingsomsing Mar 15 '17 at 20:50
  • Adding to @DYZ comment. By default Tkinter does not have any HTML rendering capabilities. – vallentin Mar 15 '17 at 20:51
  • 2
    Building a browser from scratch should be really hard. Prepare to spend half of your life doing this :P – ForceBru Mar 15 '17 at 20:51
  • Possible duplicate of [How do I open a website in a Tkinter window?](http://stackoverflow.com/questions/28799952/how-do-i-open-a-website-in-a-tkinter-window) – Noah Cristino Mar 15 '17 at 20:54
  • @DYZ I am not implementing a browser I just want to be able to create a simple browser – NeelJ 83 Mar 15 '17 at 21:18
  • 1
    What is the difference between 'implementing' and 'creating'??? – DYZ Mar 15 '17 at 21:22

2 Answers2

4

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.

Anmol Gautam
  • 949
  • 1
  • 12
  • 27
0

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()
Devco
  • 1
  • 2