PySide6 使用搜索引擎搜索 多类实现 更新1次

文章目录

使用搜索引擎搜索 多类实现

为什么写这篇文章?

这篇文章是模仿win10记事本,使用必应搜索的实现。

核心逻辑

  1. 借助QDesktopServices.openUrl()方法打开对应url网页
  2. 设置必应Bing搜索url => 字符串类型
  3. QPlainTextEditQTextEdit文本光标 (QTextCursor)的选中文本(selectedText)方法 结合2来传入搜索信息

参考

QT怎样获取TextEdit中选中的文本

修改搜索引擎

问题

QDesktopServices 目前对Wayland的支持性不好,如果是纯WaylandLinux系统(如Fedora Linux 42 (Workstation Edition))

会提示Error: Failed to open Wayland display, fallback to X11. WAYLAND_DISPLAY='wayland-0' DISPLAY=':0',并不影响使用

代码实现

行文本编辑(QLineEdit)
py 复制代码
# coding = utf-8

from PySide6.QtWidgets import (QWidget,QLineEdit,QPushButton,QVBoxLayout,QApplication)
from PySide6.QtGui import QDesktopServices
import sys

class DefindSearch(QWidget):

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.setEventBind()

    def setupUi(self):
        """设置界面
        """
        # 主布局使用垂直布局
        vbox = QVBoxLayout(self)

        # 创建文本编辑 以及 按钮
        self.lineEdit = QLineEdit()
        self.searchPushBtn = QPushButton("使用默认浏览器搜索")

        # 添加控件到部件
        vbox.addWidget(self.lineEdit)
        vbox.addWidget(self.searchPushBtn)

    def setEventBind(self):
        """设置事件绑定
        """
        self.searchPushBtn.clicked.connect(self.getHelp)
        
    def getSearchStr(self) -> str:
        """获取搜索字符串

        :return: 搜索字符串
        """
        return self.lineEdit.text()
    
    def deskSearch(self):   
        """使用默认应用查找
        """
        searchStr = self.getSearchStr()
        url = f"https://cn.bing.com/search?q={searchStr}"
        QDesktopServices.openUrl(url)

    def getHelp(self):
        """获取帮助
        """
        url = "https://gitee.com/BasterHapy/note-pad/tree/develop/"
        QDesktopServices.openUrl(url)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    deSearch = DefindSearch()
    deSearch.show()
    sys.exit(app.exec())
文本编辑(QTextEdit)
py 复制代码
# coding = utf-8

from PySide6.QtWidgets import (QWidget,QTextEdit,QPushButton,QVBoxLayout,QApplication)
from PySide6.QtGui import QDesktopServices
import sys

class DefindSearch(QWidget):

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.setEventBind()

    def setupUi(self):
        """设置界面
        """
        # 主布局使用垂直布局
        vbox = QVBoxLayout(self)

        # 创建文本编辑 以及 按钮
        self.textEdit = QTextEdit()
        self.searchPushBtn = QPushButton("使用默认浏览器搜索")

        # 添加控件到部件
        vbox.addWidget(self.textEdit)
        vbox.addWidget(self.searchPushBtn)

    def setEventBind(self):
        """设置事件绑定
        """
        self.searchPushBtn.clicked.connect(self.deskSearch)
        
    def getSearchStr(self) -> str:
        """获取搜索字符串

        :return: 搜索字符串
        """
        return self.textEdit.textCursor().selectedText()
    
    def deskSearch(self):   
        """使用默认应用查找
        """
        searchStr = self.getSearchStr()
        url = f"https://cn.bing.com/search?q={searchStr}"
        QDesktopServices.openUrl(url)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    deSearch = DefindSearch()
    deSearch.show()
    sys.exit(app.exec())
纯文本编辑器(QPlainTextEdit)

与文本编辑器类似,参考我的记事本项目

帮助网页(扩展)

实现逻辑
  • 借助QDesktopServices.openUrl()方法打开对应url网页
    参考代码

使用Python自带库实现(扩展)

示例代码
py 复制代码
import webbrowser

url = "https://www.baidu.com"

webbrowser.open(url)
相关推荐
lupai5 小时前
手机在网状态接口实测效果与质量评估
大数据·python·智能手机·api接口
荷蒲6 小时前
【小白量化Qbuddy】用AI设计miniQMT指标公式计算量化平台
人工智能·python·机器人
阿童木写作6 小时前
跨境电商图片翻译工具,批量翻译视频字幕一键抠图
人工智能·python·音视频
何以解忧,唯有..8 小时前
Pydantic 介绍与使用:Python 数据校验的现代方案
数据库·python·microsoft
又幸福了哥9 小时前
Python入门到高级(知识点七)
python
又幸福了哥9 小时前
Python入门到高级(知识点六)
python
维克兜率天9 小时前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法
XZ-07000110 小时前
week2-1-可视化
开发语言·python
Java后端的Ai之路10 小时前
14、Python - 责任链模式
服务器·开发语言·人工智能·python·责任链模式
张人玉10 小时前
基于 Python 机器学习 与 PyQt5 桌面框架开发的可视化分析系统——基于大数据的网上购物消费行为分析可视化大屏
python·qt·机器学习·echarts·three.js