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):
"""
该函数无用,当时为了测试使用,该函数的参数为window,前端传入的参数不是window,所以该函数无效
"""
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)
注:这种解决方案只是临时的一种方案,更好的解决方案暂时未找到,且这种解决方案刚好满足本人项目需求,如有更好的解决方案,请共同交流,不胜感激。
知识补充
除了上文的内容,小编还为大家整理了vue3结合pywebview实现前后端初步通信的示例代码,希望对大家有所帮助
pywebview后端
class api:
def greet(self, test_text):
print(test_text)
return f"hello, {test_text}"
if __name__ == '__main__':
# 前后端通信测试
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() {
// 调用后端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>
到此这篇关于vue3与pywebview实现获取本地文件夹的绝对路径的文章就介绍到这了,更多相关vue3获取本地文件夹的绝对路径内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论