kivy 解析 017 FileChooserBox

<MyFileChooser>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            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()

####################################################
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__':
    from kivy.core.window import Window
    Window.clearcolor = [.8, .8, .8, 1]
    FileChooserApp().run()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值