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

章节知识点总览

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

5.4.1 使用方法:

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

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty


class MyFileChooser(BoxLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)

class FileChooserBox(BoxLayout):
    loadfile = ObjectProperty(None)

    def __init__(self,**kwargs):
        super().__init__(**kwargs)

    def show_load(self):
        content = MyFileChooser(load=self.load,cancel=self.dismiss_popup)
        #打开一个弹窗
        self._popup = Popup(title='Load file',content=content,size_hint=(0.9,0.9))
        self._popup.open()

    def load(self,path,filename):
        print(path,filename)
        self.dismiss_popup()

    def dismiss_popup(self):
        #关闭弹窗
        self._popup.dismiss()
 
class FileChooserApp(App):
    def build(self):
        return FileChooserBox()

if __name__ == '__main__':
    FileChooserApp().run()
    

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

<MyFileChooser>:
	BoxLayout:
		size:root.size
		pos:root.pos
		orientation:'vertical'
		FileChooserIconView:
			id:filechooser

		BoxLayout:
			size_hint_y:None
			height:30
		
			Button:
				text:'Cancel'
				on_release:root.cancel()
			Button:
				text:'load'
				on_release:root.load(filechooser.path,filechooser.selection)
<FileChooserBox>:
	Button:
		text:'choose file'
		size_hint:.2,.1
		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选择框

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静候光阴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值