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

文章目录

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

为什么写这篇文章?

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

核心逻辑

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

参考

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

修改搜索引擎

问题

QDesktopServices 目前对Wayland的支持性不好,如果是纯Wayland的Linux系统(如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)
相关推荐
前端精髓5 小时前
结构化输出with_structured_output(抽卡)
python·langchain
尹人入圣6 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
Nuanyt7 小时前
Agent 学习笔记(下篇三):上线工程、进阶选择与操作界面(Stage 7 / 7.5 / 8 + 延伸路线)
笔记·python·学习·memory·aiagent·harness
吴卫斌7 小时前
定投ETF实战指南(四):行业/主题ETF——定投的“进攻端“
开发语言·python·股票·量化
2601_967987377 小时前
Python——入门级(定义功能def函数)
python·函数·可变参数·默认参数·关键字参数
专注仿真9 小时前
梯度提升类(XFBoost)算法详解与代码实例
开发语言·人工智能·python·算法·大模型训练·控制算法
外收内放9 小时前
Python基础语法练习题(75)
开发语言·python·学习
黑十11 小时前
PyTorch 报 “no CUDA GPUs are available“ 怎么办?3 步排查
服务器·网络·人工智能·pytorch·python
threelab11 小时前
开源图片去水印代码
python
taller_200012 小时前
【012】数据类型入门: Excel 数据到 Python 值的转换
python·excel·pie·py