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)
相关推荐
赶紧写完去睡觉1 天前
Anaconda 创建虚拟环境与使用
python·pycharm·conda
迷途呀1 天前
Python:函数中的参数类型
开发语言·笔记·python·langchain·nlp
你驴我1 天前
WhatsApp 多账号下消息已读回执的实时聚合与推送实践
后端·python
AC赳赳老秦1 天前
OpenClaw 采集任务日志审计:全程记录采集行为,满足合规溯源与企业审计要求
java·大数据·python·数据挖掘·数据分析·php·openclaw
huangdong_1 天前
电商图片下载工具横向对比深度评测:固乔、FATKUN、图快、当图、淘蛙、存图宝七款工具全面解析
python
满怀冰雪1 天前
04-Paddle Tensor 基础:形状、数据类型、广播与索引
python·深度学习·神经网络·paddle
cui_ruicheng1 天前
Python从入门到实战(十五):文件操作与目录管理
开发语言·python
xcLeigh1 天前
Doubao-Seed-Evolving大模型接入教程|搭建全品类提示词+AI工具导航网页
前端·人工智能·python·ai·html·ai开发·豆包
不如语冰1 天前
AI大模型入门-模块导入import
数据结构·人工智能·pytorch·python
投票竞赛1 天前
书法、绘画作品投票评选,图片投票小程序作品集排版
python·小程序