• 《Python+Kivy(App开发)从入门到实践》自学笔记:高级UX部件——FileChooser文件选择器


    章节知识点总览

            kivy中的FileChooser小部件提供了浏览文件的功能,它可以通过两种不同的方式(FileChooserListView图标显示,FileChooserIconView列表显示)显示文件或文件夹,这两种方式都提供了滚动和选择等基本的用户交互功能。

    5.4.1 使用方法:

            新建一个filechooser.py文件,在filechooser.py文件中定义Popup弹窗和show_load()回调方法,具体代码如下:

    1. from kivy.app import App
    2. from kivy.uix.popup import Popup
    3. from kivy.uix.boxlayout import BoxLayout
    4. from kivy.properties import ObjectProperty
    5. class MyFileChooser(BoxLayout):
    6. load = ObjectProperty(None)
    7. cancel = ObjectProperty(None)
    8. class FileChooserBox(BoxLayout):
    9. loadfile = ObjectProperty(None)
    10. def __init__(self,**kwargs):
    11. super().__init__(**kwargs)
    12. def show_load(self):
    13. content = MyFileChooser(load=self.load,cancel=self.dismiss_popup)
    14. #打开一个弹窗
    15. self._popup = Popup(title='Load file',content=content,size_hint=(0.9,0.9))
    16. self._popup.open()
    17. def load(self,path,filename):
    18. print(path,filename)
    19. self.dismiss_popup()
    20. def dismiss_popup(self):
    21. #关闭弹窗
    22. self._popup.dismiss()
    23. class FileChooserApp(App):
    24. def build(self):
    25. return FileChooserBox()
    26. if __name__ == '__main__':
    27. FileChooserApp().run()

            根据filechooser.py文件中FilechooserApp()类,新建filechooser.kv文件,内容如下:

    1. <MyFileChooser>:
    2. BoxLayout:
    3. size:root.size
    4. pos:root.pos
    5. orientation:'vertical'
    6. FileChooserIconView:
    7. id:filechooser
    8. BoxLayout:
    9. size_hint_y:None
    10. height:30
    11. Button:
    12. text:'Cancel'
    13. on_release:root.cancel()
    14. Button:
    15. text:'load'
    16. on_release:root.load(filechooser.path,filechooser.selection)
    17. <FileChooserBox>:
    18. Button:
    19. text:'choose file'
    20. size_hint:.2,.1
    21. on_release:root.show_load()

            运行filechooser.py文件,点击choose file按钮,弹出文件路径及图标,选择文件后,后台打印该文件名。结果如下:

    44b0dc1a6d6b49e8bf36ef9d606d7f41.png

     12c3f048b10b4f95a134dbe975b5e1da.png

     57c0ec8ba72f420c8025e75226911a08.png

     

     5.4.2 常用属性

    FileChooser文件选择器常用属性

    属性说明
    path从该路径下加载文件系统,默认为当前工作目录
    multiselect确定用户是否能够选择多个文件,默认为False
    dirselect确定目录是否为有效选择,默认为False
    file_encodings解码时使用的编码,默认为['utf-8','latin1','cp1252']
    file_system用于访问文件的文件系统对象,默认为:FileSystemLoad()
    files由path指定的目录中的文件列表,只读属性
    filters应用于目录中文件的过滤器,默认为[](未过滤任何内容)
    filter_dirs过滤器是否也应用于目录,默认为False
    progress_cls用于显示文件选择器加载进度指示器的类,默认为FileChooserProgress
    rootpath使用该路径替换系统根路径,默认为None
    selection当前选定文件的列表,默认为[]
    total要加载的条目总数
    manager引用ScreenManager实例
    view_list将一个list添加到FileChooser的视图列表
    view_mode当前布局的视图模式,格式为str
    getsize(fn)

    返回文件的大小(以字节为单位)

    is_dir(fn)判断是否为目录,如果是,则返回True
    is_hidden(fn)判断文件是否被隐藏,如果是,则返回True
    listdir(fn)返回目录fn中的文件列表
    cancel(*largs)取消由FileChooser启动的任何后台操作
    get_nice_size(fn)传递文件路径,返回大小
    add_widget(widget,**kwargs)添加一个新的小部件作为此小部件的子级
    on_entry_added将根级条目添加到文件列表时触发
    on_lentries_cleared清除条目列表时触发
    on_subentry_to_entry将子条目添加到现有条目或从条目中删除条目时(例如:当节点关闭时)触发
    on_submit双击选择文件时触发

    上一篇:高级UX部件——Popup弹窗

    下一篇:高级UX部件——Spinner选择框

     

     

  • 相关阅读:
    【深蓝学院】手写VIO第1章第1节
    TikTok平台广告调研分析
    uni-app 微信小程序中关于 map 地图使用案例分享
    rabbitMQ学习-发布和确认
    MATLAB switch语句
    Spring源码解析—— AOP代理的生成
    STM32F1网络编程-W5500网卡驱动移植
    contos7中mongodb数据库无法备份与还原,数据库工具的安装
    阿里云国际版回执消息简介与配置流程
    《MySQL数据库进阶实战》读后感(SQL 小虚竹)
  • 原文地址:https://blog.csdn.net/lstef/article/details/127710264