[Topic DIscussion] 360浏览器永不再见!!!108行Python代码自制浏览器
Tofloor
poster avatar
来自Ubuntu的某位用户
deepin
2022-06-07 21:13
Author
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *


import os
import sys


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.resize(2000, 1314)
        self.show()

        self.tabWidget = QTabWidget()
        self.tabWidget.setTabShape(QTabWidget.Triangular)
        self.tabWidget.setDocumentMode(True)
        self.tabWidget.setMovable(True)
        self.tabWidget.setTabsClosable(True)
        self.tabWidget.tabCloseRequested.connect(self.close_Tab)

        self.setCentralWidget(self.tabWidget)

        self.webview = WebEngineView(self)
        self.webview.load(QUrl("http://www.baidu.com"))

        self.create_tab(self.webview)

        navtb = QToolBar("Navigation")
        navtb.setIconSize(QSize(16, 16))
        self.addToolBar(navtb)

        back_btn = QAction(QIcon(os.path.join('images', 'houtui.png')), "后退", self)
        back_btn.triggered.connect(self.webview.back)
        navtb.addAction(back_btn)

        next_btn = QAction(QIcon(os.path.join('images', 'qianjin.png')), "前进", self)
        next_btn.triggered.connect(self.webview.forward)
        navtb.addAction(next_btn)

        reload_btn = QAction(QIcon(os.path.join('images', 'refresh_icon.png')), "刷新", self)
        reload_btn.triggered.connect(self.webview.reload)
        navtb.addAction(reload_btn)

        home_btn = QAction(QIcon(os.path.join('images', 'geren1.png')), "主页", self)
        navtb.addAction(home_btn)

        self.urlbar = QLineEdit()

        stop_btn = QAction(QIcon(os.path.join('images', 'cross-circle.png')), "Stop", self)
        stop_btn.triggered.connect(lambda: self.tabs.currentWidget().stop())
        navtb.addAction(stop_btn)

        self.urlbar.returnPressed.connect(self.navigate_to_url)

        navtb.addSeparator()
        navtb.addWidget(self.urlbar)

        self.webview.urlChanged.connect(self.renew_urlbar)

    def navigate_to_url(self):
        q = QUrl(self.urlbar.text())
        if q.scheme() == '':
            q.setScheme('http')
        self.webview.setUrl(q)

    def renew_urlbar(self, q):
        self.urlbar.setText(q.toString())
        self.urlbar.setCursorPosition(0)

    def create_tab(self, webview):
        self.tab = QWidget()

        self.tabWidget.addTab(self.tab, "新建标签页")
        self.tabWidget.setCurrentWidget(self.tab)

        self.Layout = QHBoxLayout(self.tab)
        self.Layout.setContentsMargins(0, 0, 0, 0)
        self.Layout.addWidget(webview)

    def close_Tab(self, index):
        if self.tabWidget.count() > 1:
            self.tabWidget.removeTab(index)
        else:
            self.close()


class WebEngineView(QWebEngineView):

    def __init__(self, mainwindow, parent=None):
        super(WebEngineView, self).__init__(parent)
        self.mainwindow = mainwindow

    def createWindow(self, QWebEnginePage_WebWindowType):
        new_webview = WebEngineView(self.mainwindow)
        self.mainwindow.create_tab(new_webview)
        return new_webview


app = QApplication(sys.argv)

browser = MainWindow()
browser.show()

sys.exit(app.exec_())

Reply Favorite View the author
All Replies
寂静的羽夏
deepin
2022-06-07 21:30
#1

这就有点标题党啦……

scream

Reply View the author
dgmenghuan
deepin
2022-06-07 21:53
#2

tail这啥意思呀,你要发就发全点的,那几个.png文件呢哥们

Reply View the author
来自Ubuntu的某位用户
deepin
2022-06-07 22:22
#3
dgmenghuan

tail这啥意思呀,你要发就发全点的,那几个.png文件呢哥们

请发挥你的想象力自己把那几个png画出来

Reply View the author
来自Ubuntu的某位用户
deepin
2022-06-07 22:34
#4
寂静的羽夏

这就有点标题党啦……

scream

有的浏览器偷窥隐私,有的浏览器强制捆绑。。。(说的就是2345和360两个流氓)

自己动手,丰衣足食

Reply View the author
神末shenmo
deepin
Spark-App
Q&A Team
2022-06-07 23:15
#5

俺的图图呢

Reply View the author
新手小白
deepin
2022-06-08 00:08
#6
It has been deleted!
来自Ubuntu的某位用户
deepin
2022-06-08 00:44
#7
神末shenmo

俺的图图呢

代码里提到的图可以自己画,然后和.py文件放在同一个目录下

Reply View the author
来自Ubuntu的某位用户
deepin
2022-06-08 00:50
#8

这些是上面的图,我画的

houtui.png

image.png

qianjing.png

image.png

geren1.png

image.png

refresh_icon.png

image.png

cross-circle.png

image.png

Reply View the author
thepoy
deepin
2022-06-08 01:24
#9

还是尽量不要标题诱导新手吧。

浏览器这个东西不用360我是可以理解的,但是除了360还有firefox、chrome、edge,随便哪一个也不是 QtWebEngine套个壳就能比得了的。

Reply View the author
神末shenmo
deepin
Spark-App
Q&A Team
2022-06-08 02:16
#10
新手小白 It has been deleted!

还有个maicss写的dtk的来着

Reply View the author
来自Ubuntu的某位用户
deepin
2022-06-08 02:45
#11
thepoy

还是尽量不要标题诱导新手吧。

浏览器这个东西不用360我是可以理解的,但是除了360还有firefox、chrome、edge,随便哪一个也不是 QtWebEngine套个壳就能比得了的。

这些浏览器都内置了谷歌,微软,火狐自家的服务,一定会增加浏览器的占用,自己写的浏览器不会附带任何商业公司的服务

(有些内置服务包括上传浏览器数据)

Reply View the author
陌生人
deepin
2022-06-08 04:47
#12

你要是个卖课的我就喷死你pride ,本质上是调用了qt里边的那个浏览器内核,实现了最简单的功能,只能说是能用,相对来讲不会有额外的数据被发送出去

Reply View the author
来自Ubuntu的某位用户
deepin
2022-06-08 05:10
#13
It has been deleted!
来自Ubuntu的某位用户
deepin
2022-06-08 05:12
#14

我们不是卖课的,只是分享下经验

Reply View the author