<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()
kivy 解析 017 FileChooserBox
最新推荐文章于 2024-05-25 07:56:05 发布