编辑
2025-04-12
前端
00

这段时间在写WeClip和一些自己想写的小工具,最近想给WeClip添加一个预览功能,打算fork一个分支添加

image.png 基本的demo已经跑通,使用webview来实现,点击一个预览按钮然后python和js进行交互就好

py
import os import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtCore import QUrl, Qt from http.server import SimpleHTTPRequestHandler from socketserver import TCPServer import threading class WebViewApp(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Markdown Editor") self.setGeometry(100, 100, 1200, 800) # 启动本地服务器 self.start_local_server() self.initUI() def start_local_server(self): dist_dir = os.path.join(os.path.dirname(__file__), "dist") os.chdir(dist_dir) # 切换到dist目录 self.server = TCPServer(("localhost", 5173), SimpleHTTPRequestHandler) self.thread = threading.Thread(target=self.server.serve_forever) self.thread.daemon = True self.thread.start() print(f"本地服务器已启动: http://localhost:5173") def initUI(self): layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) self.webview = QWebEngineView() url = QUrl("http://localhost:5173") self.webview.load(url) # 调试输出 self.webview.loadFinished.connect(self.onLoadFinished) layout.addWidget(self.webview) self.setLayout(layout) def onLoadFinished(self, ok): if ok: print("页面加载成功") self.webview.page().runJavaScript("console.log('PyQt5加载完成')") else: print("页面加载失败") def closeEvent(self, event): self.server.shutdown() self.thread.join() super().closeEvent(event) if __name__ == '__main__': QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True) QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True) app = QApplication(sys.argv) ex = WebViewApp() ex.show() sys.exit(app.exec_()

上面是pyqt5使用webview的demo

前端方面因为就一个页面直接把vue3打包成onefile了,预览库用的md-eidtor-v3,挺好看的

WeClip是一个微信公众号文章下载工具

本文作者:yowayimono

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!