Vue3與pywebview實(shí)現(xiàn)獲取本地文件夾的絕對(duì)路徑
1、Vue端
<template> <div> <button @click="selectFolder">選擇文件夾</button> <button @click="showFolder">顯示文件夾</button> <p>{{ folderPath }}</p> </div> </template> <script> export default { data() { return { folderPath: '' }; }, methods: { selectFolder() { window.pywebview.api.open_folder_dialog().then(path => { this.folderPath = path; console.log(this.folderPath); }); }, showFolder() { window.pywebview.api.show_folder_dialog().then(path => { this.folderPath = path['path_back']; console.log(this.folderPath); }); } } }; </script>
2、python端
import webview class Api: def open_folder_dialog(self, window): """ 該函數(shù)無(wú)用,當(dāng)時(shí)為了測(cè)試使用,該函數(shù)的參數(shù)為window,前端傳入的參數(shù)不是window,所以該函數(shù)無(wú)效 """ folder_path = window.create_file_dialog(webview.FOLDER_DIALOG) print(folder_path) folder_path_str = str(folder_path) print(folder_path_str, type(folder_path_str)) def show_folder_dialog(self): folder_path = root_path response = {"path_back": folder_path} return response def open_folder_dialog(window): global root_path folder_path = window.create_file_dialog(webview.FOLDER_DIALOG) print(folder_path, type(folder_path)) root_path = str(folder_path[0]) print(root_path, type(root_path)) if __name__ == '__main__': api = Api() window = webview.create_window('Vue app in pywebview', './static/index.html', js_api=api) # webview.start(api.show_folder_dialog, window, debug=True) webview.start(open_folder_dialog, window, debug=True)
注:這種解決方案只是臨時(shí)的一種方案,更好的解決方案暫時(shí)未找到,且這種解決方案剛好滿足本人項(xiàng)目需求,如有更好的解決方案,請(qǐng)共同交流,不勝感激。
知識(shí)補(bǔ)充
除了上文的內(nèi)容,小編還為大家整理了Vue3結(jié)合pywebview實(shí)現(xiàn)前后端初步通信的示例代碼,希望對(duì)大家有所幫助
pywebview后端
class Api: def greet(self, test_text): print(test_text) return f"hello, {test_text}" if __name__ == '__main__': # 前后端通信測(cè)試 api = Api() window = webview.create_window('Vue app in pywebview', './static/index.html', js_api=api) # vue的build文件的路徑 webview.start(debug=True)
Vue3前端
<template> <div id="app"> <h1>Greeting Test</h1> <button @click="greet">Greet</button> <p>{{ greeting }}</p> </div> </template> <script> export default { data() { return { greeting: '' }; }, methods: { greet() { // 調(diào)用后端API if (window.pywebview) { window.pywebview.api.greet('Socket test').then(response => { this.greeting = response; console.log(this.greeting); }); } } } }; </script> <style> #app { text-align: center; margin-top: 50px; } </style>
到此這篇關(guān)于Vue3與pywebview實(shí)現(xiàn)獲取本地文件夾的絕對(duì)路徑的文章就介紹到這了,更多相關(guān)Vue3獲取本地文件夾的絕對(duì)路徑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目首屏加載時(shí)間優(yōu)化實(shí)戰(zhàn)
單頁(yè)面應(yīng)用的一個(gè)問(wèn)題就是首頁(yè)加載東西過(guò)多,加載時(shí)間過(guò)長(zhǎng)。特別在移動(dòng)端,單頁(yè)面應(yīng)用的首屏加載優(yōu)化更是繞不開(kāi)的話題,這篇文章主要介紹了vue項(xiàng)目首屏加載時(shí)間優(yōu)化實(shí)戰(zhàn),感興趣的小伙伴們可以參考一下2019-04-04vue中keep-alive內(nèi)置組件緩存的實(shí)例代碼
這篇文章主要介紹了vue中的keep-alive內(nèi)置組件緩存,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Vue中使用Scss實(shí)現(xiàn)配置、切換主題方式
這篇文章主要介紹了Vue中使用Scss實(shí)現(xiàn)配置、切換主題方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue-cli3 設(shè)置端口號(hào)(81)無(wú)效的解決
這篇文章主要介紹了vue-cli3 設(shè)置端口號(hào)(81)無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02vue2使用思維導(dǎo)圖jsmind的詳細(xì)代碼
jsMind是一個(gè)基于Js的思維導(dǎo)圖庫(kù),jsMind是一個(gè)純JavaScript類庫(kù),用于創(chuàng)建、展示和操作思維導(dǎo)圖,這篇文章主要給大家介紹了關(guān)于vue2使用思維導(dǎo)圖jsmind的詳細(xì)代碼,需要的朋友可以參考下2024-06-06vue手寫(xiě)加載動(dòng)畫(huà)項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了vue手寫(xiě)加載動(dòng)畫(huà)項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10淺析Proxy如何實(shí)現(xiàn)Vue響應(yīng)式
這篇文章主要是來(lái)和大家探討一下,Vue的響應(yīng)式系統(tǒng)僅僅是一個(gè)Proxy嗎,本文將圍繞此問(wèn)題探索一下Proxy是如何實(shí)現(xiàn)Vue響應(yīng)式的,感興趣的小伙伴可以了解一下2023-08-08vue3中關(guān)于路由hash與History的設(shè)置
這篇文章主要介紹了vue3中關(guān)于路由hash與History的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08