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)
注:这种解决方案只是临时的一种方案,更好的解决方案暂时未找到,且这种解决方案刚好满足本人项目需求,如有更好的解决方案,请共同交流,不胜感激。