当前位置: 代码网 > it编程>前端脚本>Vue.js > Vue3与pywebview实现获取本地文件夹的绝对路径

Vue3与pywebview实现获取本地文件夹的绝对路径

2024年11月25日 Vue.js 我要评论
1、vue端<template> <div> <button @click="selectfolder">选择文件夹</button> &

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获取本地文件夹的绝对路径内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com