【问题标题】:downloading images from the file system to kivy app将图像从文件系统下载到 kivy 应用程序
【发布时间】:2020-09-12 00:03:08
【问题描述】:

我有一个简单的 python kivy 应用程序和 sqlite 数据库。

我想实现将我的图片从手机添加到应用程序的功能

当用户点击按钮时如何打开文件管理器,或任何其他方式。 该文件应上传到我的应用程序所在的文件夹(在 ./images 文件夹中),并将其名称添加到我的数据库中

这可能吗? 还是将图像上传到数据库更好? (不打算使用超过 10 张图片)

我很高兴在文档或示例中看到指向合适解决方案的链接

更新

kivy FileChooser 可以在我的 PC (ubuntu) 上运行,但在手机上它会打开一个您无法从任何地方获得的根文件夹

【问题讨论】:

    标签: python python-3.x image kivy python-imaging-library


    【解决方案1】:

    这对我有用

    from kivymd.app import MDApp
    from kivy.core.window import Window
    from kivy.lang import Builder
    from kivy.factory import Factory
    from kivy.uix.modalview import ModalView
    from plyer import storagepath
    
    from kivymd.uix.filemanager import MDFileManager
    from kivymd.theming import ThemeManager
    from kivymd.toast import toast
    
    
    Builder.load_string('''
    
    
    <ExampleFileManager@BoxLayout>
        orientation: 'vertical'
        spacing: dp(5)
    
        MDToolbar:
            id: toolbar
            title: app.title
            left_action_items: [['menu', lambda x: None]]
            elevation: 10
            md_bg_color: app.theme_cls.primary_color
    
    
        FloatLayout:
    
            MDRoundFlatIconButton:
                text: "Open manager"
                icon: "folder"
                pos_hint: {'center_x': .5, 'center_y': .6}
                on_release: app.file_manager_open()
    ''')
    
    
    class Example(MDApp):
        title = "File Manage"
    
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            Window.bind(on_keyboard=self.events)
            self.manager_open = False
            self.manager = None
    
        def build(self):
            return Factory.ExampleFileManager()
    
        def file_manager_open(self):
            if not self.manager:
                self.manager = ModalView(size_hint=(1, 1), auto_dismiss=False)
                self.file_manager = MDFileManager(
                    exit_manager=self.exit_manager, select_path=self.select_path)
                self.manager.add_widget(self.file_manager)
                self.file_manager.show(storagepath .get_home_dir())  # output manager to the screen
            self.manager_open = True
            self.manager.open()
    
        def select_path(self, path):
            '''It will be called when you click on the file name
            or the catalog selection button.
    
            :type path: str;
            :param path: path to the selected directory or file;
            '''
    
            self.exit_manager()
            toast(path)
    
        def exit_manager(self, *args):
            '''Called when the user reaches the root of the directory tree.'''
    
            self.manager.dismiss()
            self.manager_open = False
    
        def events(self, instance, keyboard, keycode, text, modifiers):
            '''Called when buttons are pressed on the mobile device..'''
    
            if keyboard in (1001, 27):
                if self.manager_open:
                    self.file_manager.back()
            return True
    
    
    Example().run()
    

    KivyMD file manager

    plyer

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 2016-03-05
      • 1970-01-01
      • 2014-07-04
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      相关资源
      最近更新 更多